重定向不再起作用了

Lin*_*der 7 ruby-on-rails ruby-on-rails-3

我正在将我的用户重定向到上一页.

这是电影控制器中的更新方法的示例.

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
Run Code Online (Sandbox Code Playgroud)

我收到了这个错误.

undefined method 'hash_for_back_url' for #<Module:0x00000103eeaaa8>respond_with行了.

我在Ruby 1.9中使用Rails 3.1 rc1.

在做这样的事情时它起作用.

respond_with(@movie, location: request.referer)

任何人都知道为什么这个:back论点不起作用?

Lin*_*der 8

对我有用的唯一解决方案是使用request.referer.

解决方案#1

respond_with(@comment, location: request.referer)
Run Code Online (Sandbox Code Playgroud)

解决方案#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"
Run Code Online (Sandbox Code Playgroud)