I''m trying to achieve something that I assume is fairly common?
I have a Category table and a Brand Table.
When I create a new Category I want a list of checkboxes to appear,
and when checked they update the category_brand table.
Here are the models.
class Category < ActiveRecord::Base
has_and_belongs_to_many :brands
end
class Brand < ActiveRecord::Base
has_and_belongs_to_many :categories
end
Here is the view to add a new category.
<h1>Add a new Category</h1>
<% form_for(@category) do |f| %>
<%= f.error_messages %>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
<h3>Add Brands required for this new category</h3>
<ul>
<% for b in @brands do -%>
<li>
<%= check_box_tag "categories[brand_ids][]",
"#{b.id}" -%>
<%= "#{b.name}" -%>
</li>
<% end -%>
</ul>
<p>
<%= f.submit "Create" %>
</p>
<% end %>
<p> </p>
<%= link_to ''Back'', categories_path %>
I have read all the documentation & tutorials I can find on this and
it is not clear to me if anything has to be done in the controller?
Some examples imply it happens because of the association and other
example have code such as:
@category.brands = brand.find(@params[:brand_ids]) if
@params[:brand_ids]
Which produces an error for me..
You have a nil object when you didn''t expect it!
TIA - Dave Porter
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Davo wrote:> @category.brands = brand.find(@params[:brand_ids]) if @params[:categories][:brand_ids]check this -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Pokkai Dokkai wrote:> Davo wrote: > >> @category.brands = brand.find(@params[:categories][:brand_ids]) if @params[:categories][:brand_ids] > > check this-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
All working, no code required in controller ( other than code mentioned in RailsCasts HABTM http://railscasts.com/episodes/17-habtm-checkboxes ) I had the naming wrong for the brands_categories table ! cheers, Dave On Oct 13, 1:28 pm, Pokkai Dokkai <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Pokkai Dokkai wrote: > > Davo wrote: > > >> -vwTnWBwdg3H0YwRsVyMH0A@public.gmane.org = brand.find(@params[:categories][:brand_ids]) if @params[:categories][:brand_ids] > > > check this > > -- > Posted viahttp://www.ruby-forum.com/.--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---