Hi, Apologies for what is a very basic question, but hopefully someone can help me understand why the following is done in the Rails scaffolding: Scaffolding creates, amongst others, these two methods in a controller: ''new'' renders the ''make new x record'' page. And ''create'' deals with the data sent back from the form on that page. My question is - why does ''new'' bother to create a new model object at all? It''s not used in the rhtml template, and a new model object is created as another new instance in ''create'' anyway. Scaffold methods for reference:>def new > @paper = Paper.new >end> def create > @paper = Paper.new(params[:paper]) > if @paper.save > flash[:notice] = ''Paper was successfully created.'' > redirect_to :action => ''list'' > else > render :action => ''new'' > end > endThanks for your insight, apologies again for what I''m assuming is a dumb question... - N -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I''m not sure about this (a really new RoR user), but I''d say RoR uses the @paper instance on the View as a container of the inserted form values, thus resending the new information back to the controller (after a Submit action) encapsulated on the object (chech the params[] hash to see how it travels). But I could be wrong, since I''m recently learning this incredible and interesting new framework :P On Oct 6, 6:38 pm, Peter Laurens <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > > Apologies for what is a very basic question, but hopefully someone can > help me understand why the following is done in the Rails scaffolding: > > Scaffolding creates, amongst others, these two methods in a controller: > ''new'' renders the ''make new x record'' page. And ''create'' deals with the > data sent back from the form on that page. > > My question is - why does ''new'' bother to create a new model object at > all? It''s not used in the rhtml template, and a new model object is > created as another new instance in ''create'' anyway. > > Scaffold methods for reference: > > >def new > > @paper = Paper.new > >end > > def create > > @paper = Paper.new(params[:paper]) > > if @paper.save > > flash[:notice] = ''Paper was successfully created.'' > > redirect_to :action => ''list'' > > else > > render :action => ''new'' > > end > > end > > Thanks for your insight, apologies again for what I''m assuming is a dumb > question... > > - N > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Mark Wilden
2007-Oct-07 00:19 UTC
Re: Newbie Q: Why does scaffolding create new on ''new''?
-----Original Message----- From: rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org [mailto:rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org] On Behalf Of Peter Laurens Subject: [Rails] Newbie Q: Why does scaffolding create new on ''new''?>My question is - why does ''new'' bother to create a new model object atall? It''s not used in the rhtml template, and a new model object is created as another new instance in ''create'' anyway.< Actually, the new model object is used in the _form.html template the scaffold generates. It''s there as ''model'' (not @model). The reason why is two-fold: 1) to present the user with a "blank" record (where "blank" may involve default values), and 2) so that both the create method and the update method work with records that have been automatically populated with field values. That''s my take, at least. ///ark --~--~---------~--~----~------------~-------~--~----~ 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi Peter Forget about Rails Scaffolding and invest your time to look at Active Scaffold :-) CCH http://cch4rails.blogspot.com On Oct 7, 7:38 am, Peter Laurens <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Hi, > > Apologies for what is a very basic question, but hopefully someone can > help me understand why the following is done in the Rails scaffolding: > > Scaffolding creates, amongst others, these two methods in a controller: > ''new'' renders the ''make new x record'' page. And ''create'' deals with the > data sent back from the form on that page. > > My question is - why does ''new'' bother to create a new model object at > all? It''s not used in the rhtml template, and a new model object is > created as another new instance in ''create'' anyway. > > Scaffold methods for reference: > > >def new > > @paper = Paper.new > >end > > def create > > @paper = Paper.new(params[:paper]) > > if @paper.save > > flash[:notice] = ''Paper was successfully created.'' > > redirect_to :action => ''list'' > > else > > render :action => ''new'' > > end > > end > > Thanks for your insight, apologies again for what I''m assuming is a dumb > question... > > - N > -- > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Luke Pearce
2007-Oct-07 08:38 UTC
Re: Newbie Q: Why does scaffolding create new on ''new''?
Peter Laurens wrote:> My question is - why does ''new'' bother to create a new model object at > all? It''s not used in the rhtml template, and a new model object is > created as another new instance in ''create'' anyway. > > Scaffold methods for reference: > >>def new >> @paper = Paper.new >>end >Yeah its because the reference is used in the new.rhtml (well actually the _form.rhtml to be precise) The standard generated form uses methods of the object (in this case Paper) to create the text fields and any error messages that may have occured. i.e. in _form.rhtml <code> <-- error_messages expects a ActiveRecord Object @paper to test for errors --> <%= error_messages_for ''paper'' %> <!--[form:paper]--> <p><label for="paper_name">Name</label><br/> <-- text_field expects @paper and will use @paper.name fill in the value --> <%= text_field ''paper'', ''name'' %></p> <!--[eoform:paper]--> </code> Hope that makes sense Luke -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Peter Laurens
2007-Oct-07 10:15 UTC
Re: Newbie Q: Why does scaffolding create new on ''new''?
> Hope that makes senseIt does! Those partials can be sneaky! Thanks for your time. -- 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I dont think this is good advice. I definitely recommend that newbies experiment with the built in scaffolding, to learn the rails conventions and fundamentals. Then, move on, remembering Rails scaffolding, you will probably write your own controllers. Or perhaps pick up a plug in like Active Scaffold (which do I use and like alot, btw) if you want ajaxy tables in your views. On Oct 7, 12:26 am, CCH <c...-Z/Eug3sLCsSPzdAsCNHSEg@public.gmane.org> wrote:> Hi Peter > > Forget about Rails Scaffolding and invest your time to look at Active > Scaffold :-) > > CCHhttp://cch4rails.blogspot.com > > On Oct 7, 7:38 am, Peter Laurens <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > wrote: > > > Hi, > > > Apologies for what is a very basic question, but hopefully someone can > > help me understand why the following is done in the Rails scaffolding: > > > Scaffolding creates, amongst others, these two methods in a controller: > > ''new'' renders the ''make new x record'' page. And ''create'' deals with the > > data sent back from the form on that page. > > > My question is - why does ''new'' bother to create a new model object at > > all? It''s not used in the rhtml template, and a new model object is > > created as another new instance in ''create'' anyway. > > > Scaffold methods for reference: > > > >def new > > > @paper = Paper.new > > >end > > > def create > > > @paper = Paper.new(params[:paper]) > > > if @paper.save > > > flash[:notice] = ''Paper was successfully created.'' > > > redirect_to :action => ''list'' > > > else > > > render :action => ''new'' > > > end > > > end > > > Thanks for your insight, apologies again for what I''m assuming is a dumb > > question... > > > - N > > -- > > 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-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---