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"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With