Ruby-on-rails 使用redirect_to后如何停止控制器执行?(使用导轨)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/820781/
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
How do I stop controller execution after using redirect_to? (Using Rails)
提问by Hamish Downer
I have a controller with multiple actions that take :year and :month as attributes from the URL. I have made a private method check_date to check the date is valid and check the date is not in the future.
我有一个带有多个操作的控制器,这些操作将 :year 和 :month 作为 URL 中的属性。我已经创建了一个私有方法 check_date 来检查日期是否有效并检查日期是否在未来。
def check_date(year, month)
if month < 1 || month > 12 || year < 2000
flash[:notice] = I18n.t 'archive.invalid_date'
redirect_to :action => 'index'
elsif year > Date.today.year || (year == Date.today.year && month > Date.today.month)
flash[:notice] = I18n.t 'archive.no_future'
redirect_to :action => 'month_index',
:year => Date.today.year,
:month => Date.today.month,
:type => params[:type]
end
end
Is there a rails way of ending controller execution after the redirect_to?
在redirect_to 之后是否有结束控制器执行的rails 方式?
Ways I can think of are either to throw an exception after the redirect_to or to return a value from check_date and check it in each action that calls it - something like
我能想到的方法是在 redirect_to 之后抛出异常,或者从 check_date 返回一个值并在调用它的每个操作中检查它 - 类似
def month_index
year = params[:year].to_i
month = params[:month].to_i
if !check_date(year, month)
return
...
end
But I wonder if there is some nice rails way of doing this. I was half hoping that having called redirect_to rails would recognise I wanted to stop, but that doesn't seem to happen.
但我想知道是否有一些不错的 Rails 方法可以做到这一点。我有点希望在调用了 redirect_to rails 后会意识到我想停止,但这似乎并没有发生。
采纳答案by kch
You probably want to use filters.
您可能想要使用过滤器。
If you call your check_dateas a before_filterin the controller, the fact that it rendered or redirected will prevent the controller from ever calling the action method. It ends there and then.
如果你在控制器中调用你的check_dateas a before_filter,它呈现或重定向的事实将阻止控制器调用 action 方法。它在那里结束。
回答by Slipp D. Thompson
You can also do:
你也可以这样做:
return redirect_to :action => 'index'
and
和
return redirect_to :action => 'month_index',
:year => Date.today.year,
:month => Date.today.month,
:type => params[:type]
since it looks nicer than putting return on its own line (IMHO).
因为它看起来比将回报放在自己的行上更好(恕我直言)。
回答by vrish88
You can throw in
你可以投
return false
wherever you want the code execution in your action to stop
您希望操作中的代码执行停止的任何位置
回答by Sniggerfardimungus
redirect_to just tells rails what to render when it finishes. Rails will get confused if you add other render or redirect_to directives after the one you really want, so just return from the controller after the redirect_to - it's the 'normal' rails way to do things.
redirect_to 只是告诉 rails 在完成时渲染什么。如果在您真正想要的指令之后添加其他 render 或 redirect_to 指令,Rails 会感到困惑,所以只需在 redirect_to 之后从控制器返回——这是“正常”的 Rails 做事方式。
回答by Tristan Tao
I think the OP is confused about the function of redirect_to.
我认为 OP 对redirect_to.
redirect_towill redirect at the end of the action. However, the rest of the controller function will execute as usual. All you have to do (as posted by other people) is to include a return, as you should any other function call.
redirect_to将在动作结束时重定向。但是,控制器功能的其余部分将照常执行。您所要做的(如其他人发布的那样)就是包含一个返回值,就像您应该做的任何其他函数调用一样。

