Hi, I can''t understand the RoR-API concerning certain form helpers. Can you maybe help me? I have a model, Film, with a habtm-relationship to the model Language. Now if I want to have a form for editing/creating a Film, I''d like to have checkboxes for each Language with those checked that are associated with the Film in question. Is there an "easy" way to do this (with a helper method similar to collection_select, maybe)? If not, could you point me in the right direction? * Do I need fields_for? * What data do I feed the checkboxes? * What is an elegant way to collect the data and insert it into the db? Any pointers would be greatly appreciated. A sleepless Daniel -- 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 -~----------~----~----~----~------~----~------~--~---
On 8/28/06, Daniel Jilg <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Hi, > > I can''t understand the RoR-API concerning certain form helpers. Can you > maybe help me? > > I have a model, Film, with a habtm-relationship to the model Language. > Now if I want to have a form for editing/creating a Film, I''d like to > have checkboxes for each Language with those checked that are associated > with the Film in question. > > Is there an "easy" way to do this (with a helper method similar to > collection_select, maybe)? If not, could you point me in the right > direction? >That''s easy enough: Controller: def edit @movie = Movie.find(params[:id]) @languages = Language.find(:all) end View: <label>Languages:</label><br/> <% @languages .each do |lang| %> <%= check_box_tag ''movie[language_ids][]'', lang.id, @movie.languages.include?(lang), :id=>"cb_#{lang.name}" %> <label for="cb_<%=lang.name%>" class="inline"><%=lang.name%></label><br/> <% end %> The trick is in the "movie[language_ids][]". Nothing else to do, just do the standard Movie.new(params[:movie]) stuff in your controller and everything should be fine. Cheers Max --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Max, you rock. I create forms like this all the time, and I kept thinking that there must be a better way - and lo and behold, there is. On 8/27/06, Max Muermann <ruby-DC/T6mWKptNg9hUCZPvPmw@public.gmane.org> wrote:> > > On 8/28/06, Daniel Jilg <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > Hi, > > > > I can''t understand the RoR-API concerning certain form helpers. Can you > > maybe help me? > > > > I have a model, Film, with a habtm-relationship to the model Language. > > Now if I want to have a form for editing/creating a Film, I''d like to > > have checkboxes for each Language with those checked that are associated > > with the Film in question. > > > > Is there an "easy" way to do this (with a helper method similar to > > collection_select, maybe)? If not, could you point me in the right > > direction? > > > > > That''s easy enough: > > Controller: > > def edit > @movie = Movie.find(params[:id]) > @languages = Language.find(:all) > end > > View: > > <label>Languages:</label><br/> > <% @languages .each do |lang| %> > <%= check_box_tag ''movie[language_ids][]'', lang.id, > @movie.languages.include?(lang), :id=>"cb_#{lang.name}" %> > <label for="cb_<%=lang.name%>" > class="inline"><%=lang.name%></label><br/> > <% end %> > > The trick is in the "movie[language_ids][]". Nothing else to do, just > do the standard Movie.new(params[:movie]) stuff in your controller and > everything should be fine. > > Cheers > Max > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Indeed you rock! Thanks for helping! -- 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 -~----------~----~----~----~------~----~------~--~---