This is more a philosophical question than anything, but there are couple of ways of handling form submissions, and I''m trying to understand the pros and cons of each. First, there''s what I''ll call the ''salted hash login generator method'', because most (all?) of its forms use this technique: def new_object if @request.method == :get @my_object = MyObject.new else @my_object = MyObject.new(params[:my_object]) if @my_object.save flash[:notice] = ''Object created.'' :redirect_to :action => ''done'' end end end (Note that I''ve heavily paraphrased the actual salted hash login generator code here.) So basically the form submits back to its own URL, and then a redirect happens if the object is successfully saved. On the other hand, Rails scaffolding tends to do this: def new_object @my_object = MyObject.new end def create_object @my_object = MyObject.new(params[:my_object]) if @my_object.save flash[:notice] = ''Object created.'' :redirect_to :action => ''done'' else render :action => ''new_object'' end end The new_object form submits to the create_object action, which either redirects elsewhere on success, or renders the new_object form on failure. So, finally, the question bit of this e-mail: Is there any particular reason that the Rails scaffolding has chosen to split the process into two steps like this? Does it have implications for browser history and the behaviour when the user hits the back button? Am I over-analysing all this and does my choice just boil down to which one I think looks neater? Cheers, Pete Yandell
I think most people prefer the 1st method...I certainly do. I imagine that the rails scaffolding is split because it allows you to easily and separably override each part of the form lifecycle. On 10/5/05, Pete Yandell <pete-AylieETRJAdBDgjK7y7TUQ@public.gmane.org> wrote:> This is more a philosophical question than anything, but there are > couple of ways of handling form submissions, and I''m trying to > understand the pros and cons of each. > > First, there''s what I''ll call the ''salted hash login generator > method'', because most (all?) of its forms use this technique: > > def new_object > if @request.method == :get > @my_object = MyObject.new > else > @my_object = MyObject.new(params[:my_object]) > if @my_object.save > flash[:notice] = ''Object created.'' > :redirect_to :action => ''done'' > end > end > end > > (Note that I''ve heavily paraphrased the actual salted hash login > generator code here.) So basically the form submits back to its own > URL, and then a redirect happens if the object is successfully saved. > > On the other hand, Rails scaffolding tends to do this: > > def new_object > @my_object = MyObject.new > end > > def create_object > @my_object = MyObject.new(params[:my_object]) > if @my_object.save > flash[:notice] = ''Object created.'' > :redirect_to :action => ''done'' > else > render :action => ''new_object'' > end > end > > The new_object form submits to the create_object action, which either > redirects elsewhere on success, or renders the new_object form on > failure. > > So, finally, the question bit of this e-mail: > > Is there any particular reason that the Rails scaffolding has chosen > to split the process into two steps like this? > > Does it have implications for browser history and the behaviour when > the user hits the back button? > > Am I over-analysing all this and does my choice just boil down to > which one I think looks neater? > > > Cheers, > > Pete Yandell > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I prefer the scaffolding method. Why write an extra if statement? Why not separate the two aspects into two methods? -- -- Tom Mornini On Oct 5, 2005, at 11:20 PM, Kyle Maxwell wrote:> I think most people prefer the 1st method...I certainly do. I imagine > that the rails scaffolding is split because it allows you to easily > and separably override each part of the form lifecycle. > > On 10/5/05, Pete Yandell <pete-AylieETRJAdBDgjK7y7TUQ@public.gmane.org> wrote: > >> This is more a philosophical question than anything, but there are >> couple of ways of handling form submissions, and I''m trying to >> understand the pros and cons of each. >> >> First, there''s what I''ll call the ''salted hash login generator >> method'', because most (all?) of its forms use this technique: >> >> def new_object >> if @request.method == :get >> @my_object = MyObject.new >> else >> @my_object = MyObject.new(params[:my_object]) >> if @my_object.save >> flash[:notice] = ''Object created.'' >> :redirect_to :action => ''done'' >> end >> end >> end >> >> (Note that I''ve heavily paraphrased the actual salted hash login >> generator code here.) So basically the form submits back to its own >> URL, and then a redirect happens if the object is successfully saved. >> >> On the other hand, Rails scaffolding tends to do this: >> >> def new_object >> @my_object = MyObject.new >> end >> >> def create_object >> @my_object = MyObject.new(params[:my_object]) >> if @my_object.save >> flash[:notice] = ''Object created.'' >> :redirect_to :action => ''done'' >> else >> render :action => ''new_object'' >> end >> end >> >> The new_object form submits to the create_object action, which either >> redirects elsewhere on success, or renders the new_object form on >> failure. >> >> So, finally, the question bit of this e-mail: >> >> Is there any particular reason that the Rails scaffolding has chosen >> to split the process into two steps like this? >> >> Does it have implications for browser history and the behaviour when >> the user hits the back button? >> >> Am I over-analysing all this and does my choice just boil down to >> which one I think looks neater? >> >> >> Cheers, >> >> Pete Yandell >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
On 6.10.2005, at 11.00, Tom Mornini wrote:> I prefer the scaffolding method. > > Why write an extra if statement?Because it''s elegant: if request.xhr? # ajax-y stuff elsif request.post? # do the tricks else # render stuff end Now that''s a block of code that can be very powerful. Why write an extra action in there when you can do the same in one.> > Why not separate the two aspects into two methods?Because it''s generally nicer to keep things that belong together in one place. In this case, you could use the same action in three cases: normal get request, form post request and XMLHttpRequest. That''s just beautiful. Feel free to disagree :-) //jarkko> > -- > -- Tom Mornini > > On Oct 5, 2005, at 11:20 PM, Kyle Maxwell wrote: > > >> I think most people prefer the 1st method...I certainly do. I >> imagine >> that the rails scaffolding is split because it allows you to easily >> and separably override each part of the form lifecycle. >> >> On 10/5/05, Pete Yandell <pete-AylieETRJAdBDgjK7y7TUQ@public.gmane.org> wrote: >> >> >>> This is more a philosophical question than anything, but there are >>> couple of ways of handling form submissions, and I''m trying to >>> understand the pros and cons of each. >>> >>> First, there''s what I''ll call the ''salted hash login generator >>> method'', because most (all?) of its forms use this technique: >>> >>> def new_object >>> if @request.method == :get >>> @my_object = MyObject.new >>> else >>> @my_object = MyObject.new(params[:my_object]) >>> if @my_object.save >>> flash[:notice] = ''Object created.'' >>> :redirect_to :action => ''done'' >>> end >>> end >>> end >>> >>> (Note that I''ve heavily paraphrased the actual salted hash login >>> generator code here.) So basically the form submits back to its own >>> URL, and then a redirect happens if the object is successfully >>> saved. >>> >>> On the other hand, Rails scaffolding tends to do this: >>> >>> def new_object >>> @my_object = MyObject.new >>> end >>> >>> def create_object >>> @my_object = MyObject.new(params[:my_object]) >>> if @my_object.save >>> flash[:notice] = ''Object created.'' >>> :redirect_to :action => ''done'' >>> else >>> render :action => ''new_object'' >>> end >>> end >>> >>> The new_object form submits to the create_object action, which >>> either >>> redirects elsewhere on success, or renders the new_object form on >>> failure. >>> >>> So, finally, the question bit of this e-mail: >>> >>> Is there any particular reason that the Rails scaffolding has chosen >>> to split the process into two steps like this? >>> >>> Does it have implications for browser history and the behaviour when >>> the user hits the back button? >>> >>> Am I over-analysing all this and does my choice just boil down to >>> which one I think looks neater? >>> >>> >>> Cheers, >>> >>> Pete Yandell >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >>> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Julian ''Julik'' Tarkhanov
2005-Oct-06 11:29 UTC
Re: Submitting forms back to the same URL
On 6-okt-2005, at 10:00, Tom Mornini wrote:> I prefer the scaffolding method. > > Why write an extra if statement? > > Why not separate the two aspects into two methods?I love the postback OTOH. With a little block in the controller you can do def edit @fo = Foo.find(params[:id] on_postback do if @fo.update_attributes(params[:fo]) redirect_to :action=>''list'' and return end end end All the rest is handled atomatically. -- Julian "Julik" Tarkhanov