【问题标题】:How to use collection_check_boxes with one to many association?如何使用具有一对多关联的 collection_check_boxes?
【发布时间】:2014-06-07 14:19:01
【问题描述】:

我有两个模型:

create_table "owners", force: true do |t|
  t.string   "name"
  t.string   "image"
  t.text     "description"
end

create_table "dogs", force: true do |t|
  t.string   "name"
  t.string   "age"
end

现在,当我想创建所有者时,我有一个 @owner 表单,并且数据库中已经有 @dogs,因此它们需要在表单中显示并在创建所有者时一起提交。我试过了,但它不起作用:

<%= f.collection_check_boxes :owner, :dog_ids, Dog.all, :id, :name %>

我收到此错误:

undefined method `merge' for :name:Symbol

如何以表格形式显示这些狗并以表格形式发送它们?谢谢!

【问题讨论】:

  • 试试:&lt;%= f.collection_check_boxes :dog_ids, Dog.all, :id, :name %&gt;
  • @WaliAli,现在它说没有这样的方法:dog_ids
  • 尝试在狗模型中定义一个方法来检索名称并使用它来代替 :name def get_name/self.name/end,

标签: ruby-on-rails checkbox ruby-on-rails-4


【解决方案1】:

此外,如果您使用的是 rails 4,您需要在您的所有者的控制器中允许狗属性为:

def owner_params
  params.require(:owner).permit(dog_ids:[])
end

【讨论】:

  • 谢谢,但上述解决方案对我不起作用。我可能做错了什么?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-10-07
  • 1970-01-01
  • 1970-01-01
  • 2021-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多