why is this required: def new @post = Post.find(params[:post_id]) @comment = @post.comments.build end instead of: def new @comment = Post.find(params[:post_id]).comments.build end is the 2nd style possible? If no, why not? if yes, why is the 1st style preferred? -- 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.
Nathan Kirk wrote:> why is this required: > > def new > @post = Post.find(params[:post_id]) > @comment = @post.comments.build > end > > instead of: > def new > @comment = Post.find(params[:post_id]).comments.build > end > > is the 2nd style possible? If no, why not? > if yes, why is the 1st style preferred?Depends on your view code. Remember that controller @instance variables are copied to the view, so that the first example would make both @post and @comment available in the view, whereas the second example would only make @comment available. 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 Laibow-Koser wrote:> Nathan Kirk wrote: > >> why is this required: >> >> def new >> @post = Post.find(params[:post_id]) >> @comment = @post.comments.build >> end >> >> instead of: >> def new >> @comment = Post.find(params[:post_id]).comments.build >> end >> >> is the 2nd style possible? If no, why not? >> if yes, why is the 1st style preferred? >> > > Depends on your view code. Remember that controller @instance variables > are copied to the view, so that the first example would make both @post > and @comment available in the view, whereas the second example would > only make @comment available. >And of course if you just had @post you could easily extract comments in the view code by @post.comments.build. @comment is really not necessary because @post gives you the handle you need. Norm> Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org >-- 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.
Norm Scherer wrote:> Marnen Laibow-Koser wrote: >>> def new >> only make @comment available. >> > And of course if you just had @post you could easily extract comments in > the view code by @post.comments.build.But you shouldn''t. That''s too much logic for the view.> @comment is really not necessary > because @post gives you the handle you need.Wrong. If you care about MVC, you shouldn''t be building objects in the view layer. (If you don''t care about MVC, you shouldn''t be using Rails.)> > NormBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org Sent from my iPhone -- 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@googlegroups.com. For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.