Ruby-on-rails redirect_to != 返回

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5743534/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:58:38  来源:igfitidea点击:

redirect_to != return

ruby-on-rails

提问by demas

I'm looking for some clarification regarding the behaviour of redirect_to.

我正在寻找有关redirect_to.

I have this code:

我有这个代码:

if some_condition
   redirect_to(path_one)
end

redirect_to(path_two)

If some_condition == trueI get this error:

如果some_condition == true我收到此错误:

Render and/or redirect were called multiple times in this action. Please note that you may only call render OR redirect, and at most once per action.

在此操作中多次调用渲染和/或重定向。请注意,您只能调用渲染或重定向,并且每个操作最多只能调用一次。

It seems that the method continues to execute after the redirect_tocall. Do I need to write code like this:

redirect_to调用后似乎方法继续执行。我是否需要编写这样的代码:

if some_condition
   redirect_to(path_one)
   return
end

redirect_to(path_two)

回答by Eimantas

Yes, you need to return from method when doing redirect. It actually only adds appropriate headers for the response object.

是的,您需要在进行重定向时从方法返回。它实际上只为响应对象添加适当的标头。

You can write more rubyish way:

你可以写更多的rubyish方式:

if some_condition
    return redirect_to(path_one)
end

redirect_to(path_two)

or other way:

或其他方式:

return redirect_to(some_condition ? path_one : path_two)

or another way:

或其他方式:

redirect_path = path_one

if some_condition
    redirect_path = path_two
end

redirect_to redirect_path

回答by Vasiliy Ermolovich

From http://api.rubyonrails.org/classes/ActionController/Base.html:

http://api.rubyonrails.org/classes/ActionController/Base.html

If you need to redirect on the condition of something, then be sure to add “and return” to halt execution.

如果您需要在某些条件下重定向,请务必添加“并返回”以停止执行。

def do_something
  redirect_to(:action => "elsewhere") and return if monkeys.nil?
  render :action => "overthere" # won't be called if monkeys is nil
end

回答by Rimian

You can also do

你也可以这样做

redirect_to path_one and return

which reads nice.

读起来不错。

回答by Artur Beljajev

It's worth noting there is noneed to returnunless you have any code after redirect_to, as in this example:

值得注意的是,除非在 之后有任何代码,否则没有必要,如本例所示:returnredirect_to

def show
  if can?(:show, :poll)
    redirect_to registrar_poll_url and return
  elsif can?(:show, Invoice)
    redirect_to registrar_invoices_url and return
  end
end

回答by Jon Schneider

To consolidate the "rubyish way" example in Eimantas' answerto two lines of code:

为了巩固 Eimantas对两行代码的回答中的“rubyish 方式”示例:

return redirect_to(path_one) if some_condition

redirect_to(path_two)

回答by webaholik

If you'd like to define the redirect within a method or helper function and early return in your controller:

如果您想在方法或辅助函数中定义重定向并在控制器中提前返回:

ActionController::Metal#performed?- Tests if render or redirect has already happened:

ActionController::Metal#performed?- 测试渲染或重定向是否已经发生:

def some_condition_checker
  redirect_to(path_one) if some_condition
end

Call it like this:

像这样调用它:

some_condition_checker; return if performed?

redirect_to(path_two)