Pass parameter to new action in Active Admin

I have two related models, Bunny has_many BunnyData (which belongs to Bunny). On the show page for a specific Bunny (in Active Admin), I want to create a link to create the associated BunnyData. I tried several different ways, without any success, and currently I'm trying to do this:

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

The created link ends somehow like:

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

But when you go to this page, the drop-down list for Bunny is set to the empty default value, and not to the Bunny name with ID 5.

Thanks in advance.

+6
source share
1 answer

Rails namespaces form fields for the data model, in this case BunnyData. In order for the form to be pre-filled, any fields provided must also include a 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 

Fields can be pre-populated by passing the hash to the path helper.

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

which generates the next path and sets the form field.

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

In the case of BunnyData, it may be slightly different due to single and multiple data forms. But this can be verified by checking the generated HTML to find the name attribute of the inputs.

+14
source

Source: https://habr.com/ru/post/969669/


All Articles