将参数传递给Active Admin中的新操作 [英] Pass a parameter to the new action in Active Admin

查看:49
本文介绍了将参数传递给Active Admin中的新操作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个相关模型,Bunny has_many BunnyData(属于Bunny)。从特定Bunny的显示页面(在Active Admin中),我想创建一个链接以创建相关的BunnyData。我尝试了几种不同的方法,但都没有成功,目前正在尝试:

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

但是当您转到该页面时,兔子的下拉菜单设置为空白默认值,而不是显示兔子的名称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.

预先感谢。

推荐答案

Rails命名空间表单字段到数据模型,在本例中为BunnyData。为了预先填写表格,提供的任何字段还必须包括名称空间。例如:

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

对于BunnyData,由于基准的单数和复数形式,它可能会略有不同。但这可以通过检查生成的HTML来找到输入的 name 属性来验证。

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.

这篇关于将参数传递给Active Admin中的新操作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆