Hello, I''m trying to work with nested models in a form view and it''s not going so smoothly. A user has many lawyers and a lawyer can take many services. I want a checkbox on which the user can select the services of each lawyer. How can I do it? This is what I''ve got so far: <%= form_for @user do |f| %> <%= f.label :email %><%= f.email_field :email %> .... <% @user.lawyers.each do |lawyer| %> <%= f.fields_for :lawyers, lawyer do |lf| %> <%= lf.label :name, "Nome: " %><%= lf.text_field :name %> <!-- Everything is working fine so far. However, I can get this to work. A lawyer can have many services: --> <% Service.all.each do |service| %> <%= check_box_tag ???, service.id, lawyer.services.include?(service) %> <% end %> <% end %> <% end %> <% end %> -- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/ihBN0tL0JToJ. For more options, visit https://groups.google.com/groups/opt_out.
I have found out that user[lawyers_attributes][0][service_ids][] works for the first lawyer (I update 0 to 1 for the second and so on). However, that seems rather ugly. Is thera a way to extract the path user[lawyers_attributes][0] from lf object? On Sunday, January 13, 2013 4:06:21 PM UTC-2, Rafael C. de Almeida wrote:> > Hello, > > I''m trying to work with nested models in a form view and it''s not going so > smoothly. A user has many lawyers and a lawyer can take many services. I > want a checkbox on which the user can select the services of each lawyer. > How can I do it? This is what I''ve got so far: > > <%= form_for @user do |f| %> > > <%= f.label :email %><%= f.email_field :email %> > > .... > > <% @user.lawyers.each do |lawyer| %> > > <%= f.fields_for :lawyers, lawyer do |lf| %> > > <%= lf.label :name, "Nome: " %><%= lf.text_field :name %> > > <!-- Everything is working fine so far. However, I can get this to work. A lawyer can have many services: --> > > <% Service.all.each do |service| %> > > <%= check_box_tag ???, service.id, lawyer.services.include?(service) %> > > <% end %> > > <% end %> > > <% end %> > > <% end %> > >-- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/ifA3umqZ9j0J. For more options, visit https://groups.google.com/groups/opt_out.
Let me send you another post the intention of documenting this issue for people from the future. The solution I''m going with for now is: substituting "???" for lf.object_name+"[service_ids][]". It may not be too pretty, but it works (don''t forget setting :service_ids in attr_accessible and accept_nested_attributes_for :services). On Sunday, January 13, 2013 7:00:55 PM UTC-2, Rafael C. de Almeida wrote:> > I have found out that user[lawyers_attributes][0][service_ids][] works for > the first lawyer (I update 0 to 1 for the second and so on). However, that > seems rather ugly. Is thera a way to extract the path > user[lawyers_attributes][0] from lf object? > > On Sunday, January 13, 2013 4:06:21 PM UTC-2, Rafael C. de Almeida wrote: >> >> Hello, >> >> I''m trying to work with nested models in a form view and it''s not going >> so smoothly. A user has many lawyers and a lawyer can take many >> services. I want a checkbox on which the user can select the services of >> each lawyer. How can I do it? This is what I''ve got so far: >> >> <%= form_for @user do |f| %> >> >> <%= f.label :email %><%= f.email_field :email %> >> >> .... >> >> <% @user.lawyers.each do |lawyer| %> >> >> <%= f.fields_for :lawyers, lawyer do |lf| %> >> >> <%= lf.label :name, "Nome: " %><%= lf.text_field :name %> >> >> <!-- Everything is working fine so far. However, I can get this to work. A lawyer can have many services: --> >> >> <% Service.all.each do |service| %> >> >> <%= check_box_tag ???, service.id, lawyer.services.include?(service) %> >> >> <% end %> >> >> <% end %> >> >> <% end %> >> >> <% end %> >> >>-- 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 To view this discussion on the web visit https://groups.google.com/d/msg/rubyonrails-talk/-/UVMKaTGdiJEJ. For more options, visit https://groups.google.com/groups/opt_out.
On Mon, Jan 14, 2013 at 5:15 AM, Rafael C. de Almeida <almeidaraf-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>wrote:> Let me send you another post the intention of documenting this issue for > people from the future. The solution I''m going with for now is: > substituting "???" for lf.object_name+"[service_ids][]". It may not be too > pretty, but it works (don''t forget setting :service_ids in attr_accessible > and accept_nested_attributes_for :services).just a reminder that you may want to test your solution for creating new records and for editing existing ones if you''re using the same partial/template for both pages (although I think it looks like it''s going to work except for the scenario that you try to edit and remove all services for a particular lawyer). also, you don''t need accepts_nested_attributes_for :services if you are using service_ids.> > > On Sunday, January 13, 2013 7:00:55 PM UTC-2, Rafael C. de Almeida wrote: >> >> I have found out that user[lawyers_attributes][**0][service_ids][] works >> for the first lawyer (I update 0 to 1 for the second and so on). However, >> that seems rather ugly. Is thera a way to extract the path >> user[lawyers_attributes][0] from lf object? >> >> On Sunday, January 13, 2013 4:06:21 PM UTC-2, Rafael C. de Almeida wrote: >>> >>> Hello, >>> >>> I''m trying to work with nested models in a form view and it''s not going >>> so smoothly. A user has many lawyers and a lawyer can take many >>> services. I want a checkbox on which the user can select the services of >>> each lawyer. How can I do it? This is what I''ve got so far: >>> >>> <%= form_for @user do |f| %> >>> >>> <%= f.label :email %><%= f.email_field :email %> >>> >>> .... >>> >>> <% @user.lawyers.each do |lawyer| %> >>> >>> <%= f.fields_for :lawyers, lawyer do |lf| %> >>> >>> <%= lf.label :name, "Nome: " %><%= lf.text_field :name %> >>> >>> <!-- Everything is working fine so far. However, I can get this to work. A lawyer can have many services: --> >>> >>> <% Service.all.each do |service| %> >>> >>> <%= check_box_tag ???, service.id, lawyer.services.include?(**service) %> >>> >>> <% end %> >>> >>> <% end %> >>> >>> <% end %> >>> >>> <% end %> >>> >>> -- > 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 > To view this discussion on the web visit > https://groups.google.com/d/msg/rubyonrails-talk/-/UVMKaTGdiJEJ. > > For more options, visit https://groups.google.com/groups/opt_out. > > >-- ------------------------------------------------------------- visit my blog at http://jimlabs.heroku.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-/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 https://groups.google.com/groups/opt_out.