如何检查另一个控制器操作是否存在重定向?

How can I check if there was a redirection on another controller action?

我正在从一个控制器重定向到另一个控制器。

redirect_to :controller => :controller_name, :action => :action_name

然后在我想检查是否有重定向的操作上

def action_name
  #check if there wass redirected
end

您始终可以对引荐来源网址执行特定检查,例如:

if request.referer =~ /controller-name-here/
  # code here
end

推荐的方法如@Dimitris 在另一个答案中所描述的如何也可以这样做

if some_condition
  flash[:redirect] = 'some value'
  redirect_to :controller => :controller_name, :action => :action_name
end

在另一个控制器中controller_name

def action_name
  if flash[:redirect] == 'value you set' # checks if came from redirection
    # your intended code here
  end
end

As values stored in flash hash persist over single redirection you can do like this however beware of flash.now as it only persist over that request only, after the request is responded flash.now values are flushed. However you can make persist key: values persistent as much as you like. For more info read this.http://guides.rubyonrails.org/action_controller_overview.html#the-flash

或者简单地说,您可以使用 Session 而不是 flash,但是您必须在工作完成后清除 session 自己。在一天结束时,flash 也存储在 session