views:

733

answers:

2

Following some code in Agile Web Development book, theres a method to redirect back to the last viewed page using redirect_to(:back), however when I do that it simply returns back to the page it was on.

Whats the best way of getting the desired result to work? Am I doing something wrong? My code:

def update
@user = current_user
if @user.update_attributes(params[:user])
  flash[:notice] = "Successfully updated profile."
  redirect_to(:back)
else
  render :action => 'edit'
end
end
+1  A: 

Isn't that what it is supposed to do? You are probably on you edit page when you click some sort of "submit" button to update the user attributes. Once this update is done, :back will take you back to the edit page (this is the last viewed page). Isnt that what you want?

If you want to go to the users page the you could do something like this

redirect_to user_path(@user)
Maulin
I figured it would redirect back to the page the user was previously on (say an article in a blog => edit profile page => back to article in a blog)
Ryan
In that case I think you'd want to redirect_to @blog
Andy Gaskell
Well, sort've, looks like I'm going to have to code some kind of session to store the last page the user was on before entering the edit profile page.
Ryan
+1  A: 

I think it's behaving as intended:

:back - Back to the page that issued the request. Useful for forms that are triggered from multiple places.

Andy Gaskell