Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 570
Linus Oleander Avatar asked May 24 '11 23:05

Linus Oleander


1 Answers

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"
like image 65
Linus Oleander Avatar answered Sep 22 '22 03:09

Linus Oleander