My first attempt at working with a new has_many relationship is throwing an error in the view when I try to create a new ''rcost'' instance linked to a ''resource'' (each resource can have multiple costs): "undefined method `rcosts_path'' for #<ActionView::Base:0x715bc60>" Here is the view (new.html.erb) code that appears to be causing the error (I do not reference rcosts_path anywhere in the view): <% form_for(@rcost) do |f| %> Here are my model declarations: class Resource < ActiveRecord::Base has_many :rcosts end class Rcost < ActiveRecord::Base belongs_to :resource end Here is my route declaration: map.resources :resources, :has_many => :rcosts I first tried the build method in my rcosts_controller: @rcost = @resource.rcosts.build Then tried new and manually setting the foreign key: @rcost = @resource.rcosts.new @rcost.resource_id = @resource.id But neither worked. I''d really appreciate it if someone could tell me what is going wrong and how I can create a new rcost instance associated with an existing resource instance. Thanks in advance! Mark -- 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.
Frederick Cheung
2010-Mar-15 09:58 UTC
Re: Undefined method when creating a has_many relationship
On Mar 15, 5:42 am, MarkB <mark_ba...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote:> My first attempt at working with a new has_many relationship is > throwing an error in the view when I try to create a new ''rcost'' > instance linked to a ''resource'' (each resource can have multiple > costs): > > "undefined method `rcosts_path'' for #<ActionView::Base:0x715bc60>" > > Here is the view (new.html.erb) code that appears to be causing the > error (I do not reference rcosts_path anywhere in the view): > > <% form_for(@rcost) do |f| %> >rcosts_path is being invoked by form_for, when it computes where the form should be posting too. Since you''ve got a nested resource you''ve got to tell form_for which Resource this @rcost belongs to. Fred -- 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, Fred - very helpful orientation. I assumed (mistakenly, obviously) that form_for would see the foreign key in @rcost and therefore know the @resource instance it should be associated with. I searched around and found two recommendations that appear to address this issue I am having: <% form_for([@resource, @rcost]) do |f| %> and <% form_for([:resource, @rcost]) do |f| %> Not sure how behavior will differ with each version, but I will try them and report back on my results. Mark On Mar 15, 2:58 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 15, 5:42 am, MarkB <mark_ba...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > > My first attempt at working with a newhas_manyrelationship is > > throwing an error in the view when I try to create a new ''rcost'' > > instance linked to a ''resource'' (each resource can have multiple > > costs): > > > "undefined method `rcosts_path'' for #<ActionView::Base:0x715bc60>" > > > Here is the view (new.html.erb) code that appears to be causing the > > error (I do not reference rcosts_path anywhere in the view): > > > <% form_for(@rcost) do |f| %> > > rcosts_path is being invoked by form_for, when it computes where the > form should be posting too. Since you''ve got a nested resource you''ve > got to tell form_for which Resource this @rcost belongs to. > > Fred-- 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.
Hi Fred, That did it: <% form_for([@resource, @rcost]) do |f| %> It''s all obvious in hindsight. Thanks again, Mark On Mar 15, 2:58 am, Frederick Cheung <frederick.che...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Mar 15, 5:42 am, MarkB <mark_ba...-PkbjNfxxIARBDgjK7y7TUQ@public.gmane.org> wrote: > > > My first attempt at working with a newhas_manyrelationship is > > throwing an error in the view when I try to create a new ''rcost'' > > instance linked to a ''resource'' (each resource can have multiple > > costs): > > > "undefined method `rcosts_path'' for #<ActionView::Base:0x715bc60>" > > > Here is the view (new.html.erb) code that appears to be causing the > > error (I do not reference rcosts_path anywhere in the view): > > > <% form_for(@rcost) do |f| %> > > rcosts_path is being invoked by form_for, when it computes where the > form should be posting too. Since you''ve got a nested resource you''ve > got to tell form_for which Resource this @rcost belongs to. > > Fred-- 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.