【问题标题】:Rails ActiveAdmin - pass polymorphic association parameters in link_to newRails ActiveAdmin - 在 link_to new 中传递多态关联参数
【发布时间】:2018-01-25 21:38:57
【问题描述】:

我是 ActiveAdmin 新手,遇到了这个问题:

我有两个模型:图片和内容。图片通过多态关联具有很多内容。

我成功地使来自图片show 页面的按钮“创建内容”在 app/admin/picture.rb 中使用以下内容在 new_admin_content 请求中将 contentable_type 和图片 ID 作为可内容传递:

action_item :new, only: :show do
  link_to "Add content", new_admin_content_path(contentable_type: "Picture", contentable: picture)
end

为了接收参数,我在 app/admin/content.rb 中写了以下内容:

permit_params :list, :of, :attributes, :on, :model, :contentable, :contentable_type

form do |f|
  f.object.contentable_type = params[:contentable_type]
  f.object.contentable = params[:contentabl]
  puts f.object.contentable_type
  puts f.object.contentable
  f.inputs "Details" do
    .
    .
    .
  end
  actions
end

但在这种情况下,我得到了这个错误:

NoMethodError in Admin::Contents#new

undefined method `primary_key' for String:Class

错误由f.object.contentable = params[:contentable]行触发

当我只将 contentable_type 传递给 f.object 而不传递 contentable 时,f.object 会在其字段中保存 contentable_type,但提交表单不会创建新记录。

如何将contentable 保存在其表单字段中并成功对表单提交执行create 操作?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 activeadmin polymorphic-associations


    【解决方案1】:

    通过将f.object.contentable = params[:contentabl]更改为f.object.contentable_id = params[:contentabl](如数据库中的列名)并将所有表单参数添加到permitted params解决了问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-16
      • 2015-11-04
      • 2014-01-18
      • 1970-01-01
      • 2011-09-19
      相关资源
      最近更新 更多