Reputation: 329
So I have a categories model
class Category < ActiveRecord::Base
has_many :provider_categories
has_many :providers, :through => :provider_categories
end
and a provider model
class Provider < ActiveRecord::Base
has_many :provider_categories
has_many :categories, :through => :provider_categories
accepts_nested_attributes_for :provider_categories, :allow_destroy => true
end
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|
...
[email protected]_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>
EDIT:
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">
EDIT 2:
I ended up using check_box_tag
[email protected]_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
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"=>"[email protected]", "address"=>"awesome land", "provider_category"=>{"category_id"=>"3", "provider_id"=>"1"}}, "commit"=>"Save", "id"=>"1"}
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])
Thanks for the help in advance
Upvotes: 1
Views: 2555
Reputation: 53048
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>
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|
and as there is no attribute named category_id
in model Category
you receive the error.
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|
UPDATE
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])
Use provider_categories_attributes
and not provider_categories
UPDATE 2
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
.
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
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
Upvotes: 1