I'm trying to use the following:
class PaymentsController < ApplicationController def addproduct (session[:products] ||= []) << params[:item] redirect_to :back end end
I got this exception:
undefined method `back_url' for #<PaymentsController:0x007ff682c467a8>
Why this is happening?
In Rails 4. x, for going back to previous page we use redirect_to :back. However sometimes we get ActionController::RedirectBackError exception when HTTP_REFERER is not present. This works well when HTTP_REFERER is present and it redirects to previous page.
request.referer gives you the previous URL or / if none. It is usually used to redirect the user back to the previous page (link) More information here. Regarding your question, it is simply returning 'dashboard' if found in request.referer .
Rails 5 has redirect_back
, instead of redirect_to :back
. It was changed as it used to raise an exception when request's HTTP_REFERER
was not present.
So use this:
redirect_back fallback_location: root_path
You can change root_path
to something else as per your requirements.
redirect_to :back
was deprecated in Rails 5.0 (see PR) and then removed in Rails 5.1
Use the following instead:
redirect_back(fallback_location: root_path)
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