0

I have a list of user emails in a bit of Javascript code in my view that I have created for my action. The view is in haml format. I want to press a submit button on a form and access this list of emails in the same action or another action.

I've tried using form_tag, form_for and just normal html forms, however I keep running into different issues. I've also tested with link_to and button_to tags.

Here's what we have for the Action:

module RailsAdmin
  module Config
    module Actions
      class BulkProperties < RailsAdmin::Config::Actions::Base
        RailsAdmin::Config::Actions.register(self)

        register_instance_option :collection do
          true
        end

        register_instance_option :http_methods do
          [:get, :put]
        end

        register_instance_option :controller do
          proc do
            @all_users = User.all
            @all_properties = UserProperty.all
            @all_roles = Role.all
            binding.pry
            if request.get? # EDIT
              respond_to do |format|
                format.html { render @action.template_name }
                format.js   { render @action.template_name, layout: false }
              end

            elsif request.put? # UPDATE
              binding.pry #Just testing this
          end
        end

        register_instance_option :link_icon do
          'icon-lock'
        end
      end
    end
  end
end

And this the part of my view giving me issues:

%form
  .multiselect-form
    .wrapper{"data-children-count" => "1"}
      %select.collection{:multiple => "multiple", :id => "multi"}
  %hr/
  %h1 List of user properties
  -@all_properties.each do |prop|
    %input{:name => prop.name+"checkbox", :type => "checkbox", :id => prop.name+"checkbox"}
    #{prop.name}
    %input{:name => prop.name, :type => "text", :id => prop.name}
    %br
  %input{:name => "submit", :type => "submit", :formaction => 'http://localhost:3000/admin/user/bulk_properties?Application=8&Company=1&locale=en', :method => 'put'}
%br/
%br/

The outcome is that I am led to a CanCan issue where I'm getting 'You are not authorized...'. This is because the url actually ends up holding the parameters and so it tries to redirect me to a webpage that doesn't exist.

1 Answer 1

1

I implemented an action that uploads a file with js to S3 and sets the value of a form field with javascript then i use that url on the rails admin action.

I'm pasting it all because in the past that would have helped me in these cases by comparing my approach to a working approach.

Here are the relevant (simplified)files:

# app/views/rails_admin/main/update_orders_and_line_items.html.haml

= form_tag(method: :post) do
  = hidden_field_tag 'uploaded-file-url', '', id: 'uploaded-file-url'
  = submit_tag 'Submit', class: 'action-button-pink'
# app/assets/javascripts/google_cloud_storage_field.js 

let url = get_url(fileInput);
let valueField   = $('#uploaded-file-url');
valueField.val(url);
# lib/rails_admin/config/actions/update_orders_and_line_items.rb

module RailsAdmin
  module Config
    module Actions
      class UpdateOrdersAndLineItems < RailsAdmin::Config::Actions::Base
        # Might cause random bugs if enabled
        register_instance_option :pjax? do
          false
        end

        register_instance_option :http_methods do
          [:post, :get]
        end

        register_instance_option :controller do
          proc do
            if request.post?
              url = params['uploaded-file-url']

              do_stuff_with(url)
            elsif request.get?
              # [...]
            end
          end
        end
      end
    end
  end
end

I believe the source of your bug is using the %form tag, the default method of a form is GET and thats why your browser tries to redirect you to some page, if you use like i did form_tag(method: :post) and make sure the emails are on the hidden_field_tag value, you'll get your list on the action.rb

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this, It's put me on the right track and I now have my params in my action!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.