Pass a parameter to the new action in Active Admin

前端 未结 1 1684
花落未央
花落未央 2021-02-06 03:01

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 rela

相关标签:
1条回答
  • 2021-02-06 03:33

    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.

    0 讨论(0)
提交回复
热议问题