Mr.D Mr.D - 4 months ago 31
Ruby Question

Rails_admin, how to create forms for custom actions?

I have generated my custom action by this link on wiki. I have created class which inherits from

RailsAdmin::Config::Actions::Base
.

I have created my view for this action in
rails_admin_my_action/app/views/rails_admin/main/my_action.html.erb


When we create forms for your controllers, we mostly use
form_for
which will go to my controller and do specific action, but I think in
rails_admin
there something else, because rails_admin uses his own controllers which is generated from
RailsAdmin::Config::Actions::Base
.

How to create forms for my custom rails_admin actions?

I'm asking this because there is no proper documentation about this.

My view for my action is like this:

<h3>Manage state of <%= @object.name %></h3>




<div class="btn-group" data-toggle="buttons">

<% @object.state_events.each do |event| %>
<label class="btn bnt-lg btn-default">
<%= form.radio_button :state, event, autocomplete: 'off' %> <%= event %>
</label>

<% end %>


</div>

<br/>

<hr/>

<%= form.submit 'Change', class: 'btn btn-lg btn-primary' %>




In
lib/my_action.rb
I just copied
register_instance_option :controller
of
edit
action.

When I submit my form. I get
ActiveModel::ForbiddenAttributesError
error.

Answer

It depends on your action, so take a look how templates are implemented. Basically form_tag my_action_path or form_for @object, url: my_action_path(@abstract_model, @object.id) should work. FYI you may define custom action right in rails_admin config:

RailsAdmin.config do |config|
  config.actions do
    member :my_action do
      only ['MyModel']
      http_methods { [:post, :get] }

      controller do
        proc do
          if request.get?
          elsif request.post?
          end
        end
      end

    end
  end
end
Comments