Redirecting back doesn't work anymore
I'm having some truble redirecting my users to the previous page.
Here is an example of an update method in the movies controller.
def update
@movie =开发者_开发问答 Movie.find(params[:id])
if @movie.update_attributes(params[:movie])
flash[:notice] = "The given movie is now updated."
end
respond_with(@movie, location: :back)
end
I'm getting this error.
undefined method 'hash_for_back_url' for #<Module:0x00000103eeaaa8>
on the respond_with
line.
I'm using Rails 3.1 rc1 with Ruby 1.9.
It works when doing something like this.
respond_with(@movie, location: request.referer)
Anyone knows why the :back
argument won't work?
The only solution that worked for me was the use of request.referer
.
Solution #1
respond_with(@comment, location: request.referer)
Solution #2
class ApplicationController < ActionController::Base
helper_method :redirect_back
def redirect_back(options = {})
if request.referer
redirect_to request.referer, options
else
redirect_to root_path, options
end
end
end
# View / controller
redirect_back notice: "Sending you back ... hopefully"
In that case respond_with is expecting :location to be the result of a url helper method like in respond_with(@movie, :location => movie_url(@movie))
You probably should be using one of this methods instead:
redirect to an information page with
redirect_to @movie
, which is equivalent toredirect_to movie_path(@movie)
render the form again with the updated data with
render :action => :edit
(:edit represent the action used to display the form)
How about this:
respond_with @movie do |format|
format.html { redirect_to :back }
end
Lets you override the default html responder so that you can use redirect_to :back
精彩评论