带有check_box循环的Rails嵌套属性 [英] Rails Nested attributes with check_box loop
问题描述
所以我有一个类别模型
class Category < ActiveRecord::Base
has_many :provider_categories
has_many :providers, :through => :provider_categories
end
和提供者模型
class Provider < ActiveRecord::Base
has_many :provider_categories
has_many :categories, :through => :provider_categories
accepts_nested_attributes_for :provider_categories, :allow_destroy => true
end
这是我的provider_category模型
here is my provider_category model
class ProviderCategory < ActiveRecord::Base
belongs_to :provider
belongs_to :category
end
我正在尝试创建一个嵌套表单,以将类别包括在提供者表单中
I'm trying to create a nested form to include the categories into the provider form
= form_for @provider do |f|
...
-@categories.each.with_index do |category,index|
=f.fields_for :provider_category, category do |pl|
=pl.check_box :category_id
=pl.label category.name
=pl.hidden_field :provider_id, value: @provider.id
当我加载得到的页面时,复选框不配合
The checkbox is not cooperating when I load the page I get
undefined method `category_id' for #<Category:0x0000010184ff60>
如果我将:category_id更改为id,那么它可以工作,但是html outpuy看起来像这样
if I change the :category_id to id it works but the html outpuy looks like this
<input name="provider[provider_category][id]" type="hidden" value="0"><input checked="checked" id="provider_provider_category_id" name="provider[provider_category][id]" type="checkbox" value="1">
<label for="provider_provider_category_Acupuncture">Acupuncture</label>
<input id="provider_provider_category_provider_id" name="provider[provider_category][provider_id]" type="hidden" value="1">
我最终使用了check_box_tag
I ended up using check_box_tag
-@categories.each.with_index do |category|
=f.fields_for :provider_category, category do |pl|
.li
=check_box_tag "provider[provider_category][category_id]", category.id
=pl.label category.name
=pl.hidden_field :provider_id, value: @provider.id
现在html输出似乎是正确的,但是没有保存.当我选择多个项目时,这是保存中的哈希值
Now the html output seems to be correct but it's not saving it. When I select multiple items this is the hash from saving
"provider"=>{"name"=>"Mr. Awesome", "phone"=>"999-999-9999", "email"=>"awesome@awesome.com", "address"=>"awesome land", "provider_category"=>{"category_id"=>"3", "provider_id"=>"1"}}, "commit"=>"Save", "id"=>"1"}
我确实选择了3个选项,但仅将最后一个添加到哈希中
I did select 3 options but only the last one was added to the hash
我还在提供者控制器的强大参数中添加了提供者类别
I also added the provider categories in the strong params of provider controller
params.require(:provider).permit(..., :provider_categories => [:category_id, :provider_id])
非常感谢您的帮助
推荐答案
根据模型定义,ProviderCategory
具有属性category_id
和provider_id
.您收到以下错误消息:
AS per your model definitions, ProviderCategory
has attributes category_id
and provider_id
. You are getting error as:
undefined method `category_id' for #<Category:0x0000010184ff60>
因为没有传递ProviderCategory
的实例,而是传递了Category
类的实例.注意category
被传入
because instead of passing instance of ProviderCategory
, you have passed an instance of Category
class. Notice category
being passed in
=f.fields_for :provider_category, category do |pl|
,并且由于模型Category
中没有名为category_id
的属性,因此会收到错误消息.
and as there is no attribute named category_id
in model Category
you receive the error.
要解决该问题,只需更新视图中的fields_for
方法调用,如下所示:
To resolve that, just update the fields_for
method call in your view as below:
=f.fields_for :provider_categories, category.provider_categories.build do |pl|
更新
此外,您还必须更新ProvidersController
的强参数,以便将provider_categories
的记录正确保存在数据库中:
Also, you would have to update the strong params of ProvidersController
so that records for provider_categories
are saved correctly in database:
params.require(:provider).permit(..., :provider_categories_attributes => [:category_id, :provider_id])
使用provider_categories_attributes
而不是provider_categories
更新2
OP还有另一个问题,因为未经检查的category_id
传递了值0
,并且在provider_categories
中创建了错误的记录,该记录的category_id
设置为0
.
OP had another problem, for the unchecked category_id
, value 0
was being passed and incorrect records with category_id
set as 0
were getting created in provider_categories
.
建议通过将reject_if
选项传递给Provider
模型中的accepts_nested_attributes_for
方法调用来拒绝这些记录,如下所示:
Suggested to reject those records by passing reject_if
option to accepts_nested_attributes_for
method call in Provider
model as below:
class Provider < ActiveRecord::Base
has_many :provider_categories
has_many :categories, :through => :provider_categories
accepts_nested_attributes_for :provider_categories, :allow_destroy => true, reject_if: proc { |attributes| attributes['category_id'] == "0" }
end
复选框未传递所检查类别的ID,建议如下更新复选框代码:
Checkbox was not passing the id's of checked categories, suggested to update the checkbox code as below:
=pl.check_box :category_id, {}, category.id, 0
这篇关于带有check_box循环的Rails嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!