Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass a parameter to the new action in Active Admin

I have two related models, Bunny has_many BunnyData (which belongs_to Bunny). From the show page of a particular Bunny (in Active Admin), I want to create a link to make a related BunnyData. I've tried a few different ways, with no success, and am currently trying this:

sidebar :data, :only => :show do
  link_to 'New Data', new_admin_bunny_datum(:bunny_id => bunny.id)
end

The link being generated ends up as something like:

.../admin/bunny_data/new?bunny_id=5

But when you go to that page, the dropdown for Bunny is set to the blank default as opposed to showing the name of Bunny with ID 5.

Thanks in advance.

like image 665
uhezay Avatar asked May 21 '14 22:05

uhezay


1 Answers

Rails namespaces form fields to the data model, in this case BunnyData. For the form to be pre-filled, any fields provided must also include the namespace. As an example:

ActiveAdmin.register Post do
  form do |f|
    f.inputs "Post Details" do
      f.input :user
      f.input :title
      f.input :content
    end
    f.actions
  end
end

The fields can be pre-filled by passing a hash to the path helper.

link_to 'New Post', new_admin_post_path(:post => { :user_id => user.id })

Which would generate the following path and set the form field.

/admin/posts/new?post[user_id]=5

In the case of BunnyData, it might be slightly different due to the singular and plural forms of datum. But that can be verified by inspecting the generated HTML to find the name attribute of the inputs.

like image 109
Charles Maresh Avatar answered Oct 11 '22 12:10

Charles Maresh