Grary
2010-Apr-26 14:29 UTC
nested resources and controllers: I think I''m down the wrong path
Hi, I''m new to nested resources. I''m surprised to find that to make my code work I need to retrieve the parent model in my RESTful controllers for my dependencies. So, for example, let''s say A has_one B. In controller B I seem to need to refer to parent A, for example: @b = A.find(params[:a_id]).b But this doesn''t seem right and this change has only seemed to become necessary as I''ve lately been using nested routes and nested form_for stuff. Can anyone confirm that using nested routes should not, in fact, require wholesale changes like the one above to dependency controllers? Thanks, Grar -- 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.
Marnen Laibow-Koser
2010-Apr-26 14:35 UTC
Re: nested resources and controllers: I think I''m down the w
Grary Stimon wrote:> Hi, > > I''m new to nested resources. I''m surprised to find that to make my > code work I need to retrieve the parent model in my RESTful > controllers for my dependencies. > > So, for example, let''s say A has_one B. In controller B I seem to need > to refer to parent A, for example: > > @b = A.find(params[:a_id]).b > > But this doesn''t seem rightNo, it doesn''t. You should always be able to do B.find(params[:b_id]) . What happens when you try? Best, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/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.
Marnen, Here is the expression of a representative model association in my project: Parent ... has_one :dependent Dependent ... belongs_to :parent Here is my complementing route: map.resources :parents, :has_one => [:dependent, :dependent_2, ... Here is a change I''d lately made to a _form.html.erb partial in views/ children to make things (kind of) work: <% form_for([:parent, @child], :url => { :action=>:create }) do |f| %> Man, I''m vexed! I feel like I need to understand how form_for must be adapted for use with nested resources. Grar On Apr 26, 10:35 am, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> Grary Stimon wrote: > > Hi, > > > I''m new to nested resources. I''m surprised to find that to make my > > code work I need to retrieve the parent model in my RESTful > > controllers for my dependencies. > > > So, for example, let''s say A has_one B. In controller B I seem to need > > to refer to parent A, for example: > > > @b = A.find(params[:a_id]).b > > > But this doesn''t seem right > > No, it doesn''t. You should always be able to do B.find(params[:b_id]) . > What happens when you try? > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://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-/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.
Marnen Laibow-Koser
2010-Apr-26 15:06 UTC
Re: nested resources and controllers: I think I''m down the w
[Please quote when replying.] Grary Stimon wrote:> Marnen, > > Here is the expression of a representative model association in my > project: > > Parent ... has_one :dependent > Dependent ... belongs_to :parent > > Here is my complementing route: > > map.resources :parents, :has_one => [:dependent, :dependent_2, ...Why the :dependent_2 ? That doesn''t match your associations above. And do you really want has_one? It has legitimate uses, but n most cases, you should combine the two models instead of using has_one .> > Here is a change I''d lately made to a _form.html.erb partial in views/ > children to make things (kind of) work: > > <% form_for([:parent, @child], :url => { :action=>:create }) do |f| %> > > Man, I''m vexed!Perhaps if you were to answer the question I asked, I could help you better. Perhaps if you could say what you had already tried, and how it failed, we could help you better. But with the information you are providing, and your failure to answer questions intended to gather additional information about the problem, it is nearly impossible to help.> > I feel like I need to understand how form_for must be adapted for use > with nested resources. > > GrarBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- 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-/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.
I can''t recreate the problem, somehow, let''s consider the question dead until I can. Thanks, Grar On Apr 26, 11:06 am, Marnen Laibow-Koser <li...-fsXkhYbjdPsEEoCn2XhGlw@public.gmane.org> wrote:> [Please quote when replying.] > > Grary Stimon wrote: > > Marnen, > > > Here is the expression of a representative model association in my > > project: > > > Parent ... has_one :dependent > > Dependent ... belongs_to :parent > > > Here is my complementing route: > > > map.resources :parents, :has_one => [:dependent, :dependent_2, ... > > Why the :dependent_2 ? That doesn''t match your associations above. > > And do you really want has_one? It has legitimate uses, but n most > cases, you should combine the two models instead of using has_one . > > > > > Here is a change I''d lately made to a _form.html.erb partial in views/ > > children to make things (kind of) work: > > > <% form_for([:parent, @child], :url => { :action=>:create }) do |f| %> > > > Man, I''m vexed! > > Perhaps if you were to answer the question I asked, I could help you > better. > > Perhaps if you could say what you had already tried, and how it failed, > we could help you better. > > But with the information you are providing, and your failure to answer > questions intended to gather additional information about the problem, > it is nearly impossible to help. > > > > > I feel like I need to understand how form_for must be adapted for use > > with nested resources. > > > Grar > > Best, > -- > Marnen Laibow-Koserhttp://www.marnen.org > mar...-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted viahttp://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-/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.