rails redirect_to :back not working

redirect, ruby, ruby-on-rails

Solution

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.

Problem

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?

Original source