acreadinglist
2010-Feb-06 08:56 UTC
accepts_nested_attributes_for with has_many => :through
I have two models, links and tags, associated through a 3rd model, link_tags. I added the following to my link model, has_many :tags, :through => :link_tags has_many :link_tags accepts_nested_attributes_for :tags, :allow_destroy => :false, :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } and put this in a partial called by the new and edit forms for links, <%= f.error_messages %> <p> <%= f.label :uri %><br /> <%= f.text_field :uri %> </p> <p> <%= f.label :title %><br /> <%= f.text_field :title %> </p> <h2>Tags</h2> <% f.fields_for :tags do |tag_form| %> <p> <%= tag_form.label :name, ''Tag:'' %> <%= tag_form.text_field :name %> </p> <% unless tag_form.object.nil? || tag_form.object.new_record? %> <p> <%= tag_form.label :_delete, ''Remove:'' %> <%= tag_form.check_box :_delete %> </p> <% end %> <% end %> <p> <%= f.submit ''Update'' %> </p> The POST on submit fails with the following error: Tag(#-621698598) expected, got Array(#-609734898) Does anyone know the necessary steps to do this? I''ve been basing it off the multi-model section of this: http://guides.rubyonrails.org/getting_started.html , but it seems incomplete for has_many => :through. Thanks, Andrew -- 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.
tommy xiao
2010-Feb-06 12:06 UTC
Re: accepts_nested_attributes_for with has_many => :through
have a look: http://railscasts.com/episodes/73-complex-forms-part-1 http://railscasts.com/episodes/73-complex-forms-part-2 http://railscasts.com/episodes/73-complex-forms-part-3 2010/2/6 acreadinglist <andrew.cove-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>> I have two models, links and tags, associated through a 3rd model, > link_tags. I added the following to my link model, > > has_many :tags, :through => :link_tags > has_many :link_tags > > accepts_nested_attributes_for :tags, :allow_destroy => :false, > :reject_if => proc { |attrs| attrs.all? { |k, v| v.blank? } } > > and put this in a partial called by the new and edit forms for links, > > <%= f.error_messages %> > > <p> > <%= f.label :uri %><br /> > <%= f.text_field :uri %> > </p> > <p> > <%= f.label :title %><br /> > <%= f.text_field :title %> > </p> > > <h2>Tags</h2> > <% f.fields_for :tags do |tag_form| %> > <p> > <%= tag_form.label :name, ''Tag:'' %> > <%= tag_form.text_field :name %> > </p> > <% unless tag_form.object.nil? || tag_form.object.new_record? %> > <p> > <%= tag_form.label :_delete, ''Remove:'' %> > <%= tag_form.check_box :_delete %> > </p> > <% end %> > <% end %> > > <p> > <%= f.submit ''Update'' %> > </p> > > The POST on submit fails with the following error: > > Tag(#-621698598) expected, got Array(#-609734898) > > Does anyone know the necessary steps to do this? I''ve been basing it > off the multi-model section of this: > http://guides.rubyonrails.org/getting_started.html > , but it seems incomplete for has_many => :through. > > Thanks, > Andrew > > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org<rubyonrails-talk%2Bunsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org> > . > For more options, visit this group at > http://groups.google.com/group/rubyonrails-talk?hl=en. > >-- tommy xiao E-mail: xiaods(AT)gmail.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 this group at http://groups.google.com/group/rubyonrails-talk?hl=en.