Frank Kim
2005-May-11 20:34 UTC
noob question: when do you use redirect_to vs render_action
Hi, I was just wondering what is the rule of thumb for when you should do a redirect_to vs render_action. For example the scaffold generator creates this update function in the controller. The update is called by the form in the edit page. Any clarification for this newbie would be appreciated. def update @user = User.find(@params[:id]) if @user.update_attributes(@params[:user]) flash[''notice''] = ''User was successfully updated.'' redirect_to :action => ''show'', :id => @user else render_action ''edit'' end end
Micah Calabrese
2005-May-11 20:50 UTC
Re: noob question: when do you use redirect_to vs render_action
Frank, The two main reasons for redirecting over rendering after the successful update are to keep the url consistent with the page you are viewing and remove and post variables so if the resulting page is reloaded the record isn''t updated again. Micah On 5/11/05, Frank Kim <mtmusko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi, > I was just wondering what is the rule of thumb for when you should do > a redirect_to vs render_action. For example the scaffold generator > creates this update function in the controller. The update is called > by the form in the edit page. > Any clarification for this newbie would be appreciated. > > def update > @user = User.find(@params[:id]) > if @user.update_attributes(@params[:user]) > flash[''notice''] = ''User was successfully updated.'' > redirect_to :action => ''show'', :id => @user > else > render_action ''edit'' > end > end > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Jarkko Laine
2005-May-12 05:44 UTC
Re: noob question: when do you use redirect_to vs render_action
On 11.5.2005, at 23:50, Micah Calabrese wrote:> Frank, > The two main reasons for redirecting over rendering after the > successful update are to keep the url consistent with the page you are > viewing and remove and post variables so if the resulting page is > reloaded the record isn''t updated again.Right. And the reason why we render_action in case of failed save is that we already have all those form parameters at hand in this action so there''s no idea to redirect and pass all of them around. We can just render a template and it will be pre-filled with the (possibly invalid) form values and possible error messages for them. //jarkko> > Micah > > On 5/11/05, Frank Kim <mtmusko-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hi, >> I was just wondering what is the rule of thumb for when you should do >> a redirect_to vs render_action. For example the scaffold generator >> creates this update function in the controller. The update is called >> by the form in the edit page. >> Any clarification for this newbie would be appreciated. >> >> def update >> @user = User.find(@params[:id]) >> if @user.update_attributes(@params[:user]) >> flash[''notice''] = ''User was successfully updated.'' >> redirect_to :action => ''show'', :id => @user >> else >> render_action ''edit'' >> end >> end >> _______________________________________________ >> 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