rails redirect_to:返回不工作

时间:2017-05-21 15:28:25

标签: ruby-on-rails ruby redirect

我正在尝试使用以下内容:

D:\Work\Learn\Laravel\Chamber\Frontend>ng -v
Your global Angular CLI version (1.0.3) is greater than your local
version (1.0.1). The local Angular CLI version is used.

To disable this warning use "ng set --global warnings.versionMismatch=false".
    _                      _                 ____ _     ___
   / \   _ __   __ _ _   _| | __ _ _ __     / ___| |   |_ _|
  / △ \ | '_ \ / _` | | | | |/ _` | '__|   | |   | |    | |
 / ___ \| | | | (_| | |_| | | (_| | |      | |___| |___ | |
/_/   \_\_| |_|\__, |\__,_|_|\__,_|_|       \____|_____|___|
               |___/
@angular/cli: 1.0.1
node: 6.10.0
os: win32 x64
@angular/common: 4.1.0
@angular/compiler: 4.1.0
@angular/core: 4.1.0
@angular/forms: 4.1.0
@angular/http: 4.0.3
@angular/platform-browser: 4.1.0
@angular/platform-browser-dynamic: 4.1.0
@angular/router: 4.1.0
@angular/cli: 1.0.1
@angular/compiler-cli: 4.1.0

D:\Work\Learn\Laravel\Chamber\Frontend>

我遇到了这个例外:

class PaymentsController < ApplicationController

  def addproduct
     (session[:products] ||= []) << params[:item]
     redirect_to :back
  end

 end

为什么会这样?

2 个答案:

答案 0 :(得分:70)

Rails 5有redirect_back,而不是redirect_to :back。当请求的HTTP_REFERER不存在时,它被用来引发异常。

所以使用这个:

redirect_back fallback_location: root_path

您可以根据自己的要求将root_path更改为其他内容。

答案 1 :(得分:8)

redirect_to :back在Rails 5.0中已弃用(请参阅PR),已在Rails 5.1中删除

请改用以下内容:

redirect_back(fallback_location: root_path)