Hey, I followed Ryan Bates tutorials here about implemting checkboxes with has and belongs to many relationships. http://railscasts.com/episodes/17-habtm-checkboxes In case you dont have time to watch it, heres a quick summary-- ---------------------------------------------------------------------------- Products and categories have the has and belongs to many relationship between them. Then, in the products form, you can put this for creating check boxes, the ultimate goal of assigning this product with many categories-- <% for category in Category.find(:all) %> <div> <%= check_box_tag "product[category_ids][]", category.id, @product.categories.include?(category) %> <%= category.name %> </div> <% end %> --------------------------------------------------------------- Ok great, that works. the checkboxes all submit. It works great. But my question is-- How can I display what categories a certain product belongs to? Most likely using a FOR loop, but I''m not too sure how to make one that displays the categories of a product. Thanks, and feel free to ask more questions if u dont understand :) thanks -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
<%= @product.categories.map(&:name).to_sentence %> On Apr 15, 5:38 pm, David Zhu <dzwestwindso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey, > > I followed Ryan Bates tutorials here about implemting checkboxes with > has and belongs to many relationships. > > http://railscasts.com/episodes/17-habtm-checkboxes > > In case you dont have time to watch it, heres a quick summary-- > > ---------------------------------------------------------------------------- > > Products and categories have the has and belongs to many relationship > between them. > > Then, in the products form, you can put this for creating check boxes, > the ultimate goal of assigning this product with many categories-- > > <% for category in Category.find(:all) %> > <div> > <%= check_box_tag "product[category_ids][]", category.id, > @product.categories.include?(category) %> > <%= category.name %> > </div> > <% end %> > > --------------------------------------------------------------- > > Ok great, that works. > > the checkboxes all submit. It works great. > > But my question is-- > > How can I display what categories a certain product belongs to? Most > likely using a FOR loop, but I''m not too sure how to make one that > displays the categories of a product. > > Thanks, and feel free to ask more questions if u dont understand :) > thanks > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Hmmm thanks for the reply.. but nothing is showing up. no error message though... is there a way to do this with a for loop? I dont understand that peice of code.. im a rails noobie. could you breifly explain? Thanks On Apr 15, 7:06 pm, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> <%= @product.categories.map(&:name).to_sentence %> > > On Apr 15, 5:38 pm, David Zhu <dzwestwindso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hey, > > > I followed Ryan Bates tutorials here about implemting checkboxes with > > has and belongs to many relationships. > > >http://railscasts.com/episodes/17-habtm-checkboxes > > > In case you dont have time to watch it, heres a quick summary-- > > > ---------------------------------------------------------------------------- > > > Products and categories have the has and belongs to many relationship > > between them. > > > Then, in the products form, you can put this for creating check boxes, > > the ultimate goal of assigning this product with many categories-- > > > <% for category in Category.find(:all) %> > > <div> > > <%= check_box_tag "product[category_ids][]", category.id, > > @product.categories.include?(category) %> > > <%= category.name %> > > </div> > > <% end %> > > > --------------------------------------------------------------- > > > Ok great, that works. > > > the checkboxes all submit. It works great. > > > But my question is-- > > > How can I display what categories a certain product belongs to? Most > > likely using a FOR loop, but I''m not too sure how to make one that > > displays the categories of a product. > > > Thanks, and feel free to ask more questions if u dont understand :) > > thanks > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
This is saying that a product has many categories. The category name will map to an array then the .to_sentence outputs each item as 1,2,3. nicely. On Apr 15, 6:42 pm, David Zhu <dzwestwindso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hmmm thanks for the reply.. but nothing is showing up. no error > message though... is there a way to do this with a for loop? > > I dont understand that peice of code.. im a rails noobie. could you > breifly explain? Thanks > > On Apr 15, 7:06 pm, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > <%= @product.categories.map(&:name).to_sentence %> > > > On Apr 15, 5:38 pm, David Zhu <dzwestwindso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hey, > > > > I followed Ryan Bates tutorials here about implemting checkboxes with > > > has and belongs to many relationships. > > > >http://railscasts.com/episodes/17-habtm-checkboxes > > > > In case you dont have time to watch it, heres a quick summary-- > > > > ---------------------------------------------------------------------------- > > > > Products and categories have the has and belongs to many relationship > > > between them. > > > > Then, in the products form, you can put this for creating check boxes, > > > the ultimate goal of assigning this product with many categories-- > > > > <% for category in Category.find(:all) %> > > > <div> > > > <%= check_box_tag "product[category_ids][]", category.id, > > > @product.categories.include?(category) %> > > > <%= category.name %> > > > </div> > > > <% end %> > > > > --------------------------------------------------------------- > > > > Ok great, that works. > > > > the checkboxes all submit. It works great. > > > > But my question is-- > > > > How can I display what categories a certain product belongs to? Most > > > likely using a FOR loop, but I''m not too sure how to make one that > > > displays the categories of a product. > > > > Thanks, and feel free to ask more questions if u dont understand :) > > > thanks > > > > -- > > > 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. > > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
Thanks, but are you sure this works with has and belongs to many? On Apr 15, 8:09 pm, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This is saying that a product has many categories. The category name > will map to an array then the .to_sentence outputs each item as > 1,2,3. nicely. > > On Apr 15, 6:42 pm, David Zhu <dzwestwindso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hmmm thanks for the reply.. but nothing is showing up. no error > > message though... is there a way to do this with a for loop? > > > I dont understand that peice of code.. im a rails noobie. could you > > breifly explain? Thanks > > > On Apr 15, 7:06 pm, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > <%= @product.categories.map(&:name).to_sentence %> > > > > On Apr 15, 5:38 pm, David Zhu <dzwestwindso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > Hey, > > > > > I followed Ryan Bates tutorials here about implemting checkboxes with > > > > has and belongs to many relationships. > > > > >http://railscasts.com/episodes/17-habtm-checkboxes > > > > > In case you dont have time to watch it, heres a quick summary-- > > > > > ---------------------------------------------------------------------------- > > > > > Products and categories have the has and belongs to many relationship > > > > between them. > > > > > Then, in the products form, you can put this for creating check boxes, > > > > the ultimate goal of assigning this product with many categories-- > > > > > <% for category in Category.find(:all) %> > > > > <div> > > > > <%= check_box_tag "product[category_ids][]", category.id, > > > > @product.categories.include?(category) %> > > > > <%= category.name %> > > > > </div> > > > > <% end %> > > > > > --------------------------------------------------------------- > > > > > Ok great, that works. > > > > > the checkboxes all submit. It works great. > > > > > But my question is-- > > > > > How can I display what categories a certain product belongs to? Most > > > > likely using a FOR loop, but I''m not too sure how to make one that > > > > displays the categories of a product. > > > > > Thanks, and feel free to ask more questions if u dont understand :) > > > > thanks > > > > > -- > > > > 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@googlegroups.com. > > > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > > > -- > > > 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. > > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.
If you have a product instance you can do @product.categories correct which returns an array of categories? On Apr 15, 8:48 pm, David Zhu <dzwestwindso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thanks, but are you sure this works with has and belongs to many? > > On Apr 15, 8:09 pm, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > This is saying that a product has many categories. The category name > > will map to an array then the .to_sentence outputs each item as > > 1,2,3. nicely. > > > On Apr 15, 6:42 pm, David Zhu <dzwestwindso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > Hmmm thanks for the reply.. but nothing is showing up. no error > > > message though... is there a way to do this with a for loop? > > > > I dont understand that peice of code.. im a rails noobie. could you > > > breifly explain? Thanks > > > > On Apr 15, 7:06 pm, Me <chabg...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > <%= @product.categories.map(&:name).to_sentence %> > > > > > On Apr 15, 5:38 pm, David Zhu <dzwestwindso...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > > Hey, > > > > > > I followed Ryan Bates tutorials here about implemting checkboxes with > > > > > has and belongs to many relationships. > > > > > >http://railscasts.com/episodes/17-habtm-checkboxes > > > > > > In case you dont have time to watch it, heres a quick summary-- > > > > > > ---------------------------------------------------------------------------- > > > > > > Products and categories have the has and belongs to many relationship > > > > > between them. > > > > > > Then, in the products form, you can put this for creating check boxes, > > > > > the ultimate goal of assigning this product with many categories-- > > > > > > <% for category in Category.find(:all) %> > > > > > <div> > > > > > <%= check_box_tag "product[category_ids][]", category.id, > > > > > @product.categories.include?(category) %> > > > > > <%= category.name %> > > > > > </div> > > > > > <% end %> > > > > > > --------------------------------------------------------------- > > > > > > Ok great, that works. > > > > > > the checkboxes all submit. It works great. > > > > > > But my question is-- > > > > > > How can I display what categories a certain product belongs to? Most > > > > > likely using a FOR loop, but I''m not too sure how to make one that > > > > > displays the categories of a product. > > > > > > Thanks, and feel free to ask more questions if u dont understand :) > > > > > thanks > > > > > > -- > > > > > 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@googlegroups.com. > > > > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > > > > -- > > > > 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@googlegroups.com. > > > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > > > -- > > > 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-/JYPxA39Uh4Ykp1iOSErHA@public.gmane.orgm. > > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > > -- > > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en. > > -- > 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org > To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com. > For more options, visit this group athttp://groups.google.com/group/rubyonrails-talk?hl=en.-- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@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.