Hi there, I have 2 actions that are rather identical: # Put /account/you/update_password def update_password @user = current :user respond_to do |format| if @user.update_attributes(params[:user]) format.html {redirect_to account_url} else format.html {render :action => "edit_password"} end end end # PUT /account/you # Update the user object. def update @user = current :user respond_to do |format| if @user.update_attributes(params[:user]) format.html {redirect_to account_url} else format.html {render :action => "edit"} end end end The only thing that differs between the 2 is in the "else" part {render :action => "edit" or render :action => "edit_password"} lines. In the past when I have encountered a situation like this I''d use one action, and in the forms I''d stick an extra param indicating what action to render if an error occurred. Doing that seemed kind of clunky in my opinion. This seems like something common, and/or simple that I''m just overlooking. Is going with the extra param in the form the best route? Thanks, Dave --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
MichaelLatta
2007-May-03 15:53 UTC
Re: Multiple Update Actions that are Relatively the same
I do not know if having 2 methods is best, but to reduce the duplication you could always create a utility method that does all the common part and takes a block for the unique part, then evaluates the block in the else clause. Michael On May 3, 7:51 am, "Dave Hoefler" <dhoef...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi there, > > I have 2 actions that are rather identical: > > # Put /account/you/update_password > def update_password > @user = current :user > respond_to do |format| > if @user.update_attributes(params[:user]) > format.html {redirect_to account_url} > else > format.html {render :action => "edit_password"} > end > end > end > > # PUT /account/you > # Update the user object. > def update > @user = current :user > respond_to do |format| > if @user.update_attributes(params[:user]) > format.html {redirect_to account_url} > else > format.html {render :action => "edit"} > end > end > end > > The only thing that differs between the 2 is in the "else" part {render > :action => "edit" or render :action => "edit_password"} lines. In the past > when I have encountered a situation like this I''d use one action, and in the > forms I''d stick an extra param indicating what action to render if an error > occurred. Doing that seemed kind of clunky in my opinion. > > This seems like something common, and/or simple that I''m just overlooking. > Is going with the extra param in the form the best route? > > Thanks, > Dave--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Dave Hoefler
2007-May-03 16:06 UTC
Re: Multiple Update Actions that are Relatively the same
Sounds like a good idea. I was considering something like that. I think that might be the best route. -Dave On 5/3/07, MichaelLatta <lattam-ee4meeAH724@public.gmane.org> wrote:> > > I do not know if having 2 methods is best, but to reduce the > duplication you could always create a utility method that does all the > common part and takes a block for the unique part, then evaluates the > block in the else clause. > > Michael > > On May 3, 7:51 am, "Dave Hoefler" <dhoef...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Hi there, > > > > I have 2 actions that are rather identical: > > > > # Put /account/you/update_password > > def update_password > > @user = current :user > > respond_to do |format| > > if @user.update_attributes(params[:user]) > > format.html {redirect_to account_url} > > else > > format.html {render :action => "edit_password"} > > end > > end > > end > > > > # PUT /account/you > > # Update the user object. > > def update > > @user = current :user > > respond_to do |format| > > if @user.update_attributes(params[:user]) > > format.html {redirect_to account_url} > > else > > format.html {render :action => "edit"} > > end > > end > > end > > > > The only thing that differs between the 2 is in the "else" part {render > > :action => "edit" or render :action => "edit_password"} lines. In the > past > > when I have encountered a situation like this I''d use one action, and in > the > > forms I''d stick an extra param indicating what action to render if an > error > > occurred. Doing that seemed kind of clunky in my opinion. > > > > This seems like something common, and/or simple that I''m just > overlooking. > > Is going with the extra param in the form the best route? > > > > Thanks, > > Dave > > > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---