Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Active Admin - using the Form DSL in a member action (with has_many)

Is there any way of using ActiveAdmin's form dsl from a custom member action?

I want to retain the has_many semantic to avoid having to implement it myself from scratch, but I want a separate form view.

Something like this would be ideal:

member_action :subject, method: :get do
  @subject = Subject.find(params[:id])

  form do |f|
    f.inputs do
      f.input :name, :required => true, :input_html => {:class => "large"}      
    end
  end
end
like image 396
Toby Hede Avatar asked Nov 03 '22 17:11

Toby Hede


1 Answers

Arbre doesn't seem to support formtastic, so I wasn't able to get a form to work in a .arb file. You can use formtastic in a .erb file however. So while it is not using ActiveAdmin's DSL wrapper for formtastic, the syntax is similar:

# app/admin/foo.rb

ActiveAdmin.register Foo do
  member_action :subject, method: [:get, :patch] do
    if request.get?
      render :some_custom_view
    else
      # handle update or whatever else you would like to do on form submit
      # if resource.update(...)
      #   redirect_to ...
      # else
      #   render :some_custom_view
      # end
    end
  end
end

# app/views/admin/foo/some_custom_view.html.erb

<%= semantic_form_for [:admin, resource], url: subject_admin_foo_path(resource) do |f| %>
  <%= f.semantic_errors(*f.object.errors.keys) %>
  <%= f.inputs do %>
    <%= f.input :name %>
  <% end %>
  <%= f.actions %>
<% end %>
like image 111
littleforest Avatar answered Nov 11 '22 08:11

littleforest