[英]Rails: Redirecting from one controller action to another (non-GET)
I have a GET route which, before rendering the view, the Controller checks for an optional param. 我有一个GET路由,在呈现视图之前,Controller会检查可选参数。 If it's there, I'd like to bypass rendering the view and "redirect" straight to another controller action.
如果有,我想绕过渲染视图,直接“重定向”到另一个控制器动作。 The other action is a PUT route and I realize we can't simply redirect to PUT routes as you could to a GET route.
另一个动作是PUT路由,我意识到我们不能像重定向到GET路由那样简单地重定向到PUT路由。
This is roughly what I've attempted so far but I don't know how to invoke the redirect, as it's impossible with PUT. 到目前为止,这大致就是我尝试过的方法,但是我不知道如何调用重定向,因为使用PUT是不可能的。 Perhaps there's some different design pattern for handling this sort of behavior?
也许有一些不同的设计模式来处理这种行为? Thanks in advance.
提前致谢。
Controller actions:
def foo
if 'XYZ'
# "redirect" to bar
else
render 'view'
end
end
#bar is a PUT route
def bar
...
end
If you have your routes for bar
action something like this 如果你有你的路线
bar
行动是这样的
match '/controller/bar/' => 'controller#bar'
modify it like 修改像
match '/controller/bar/' => 'controller#bar' , :via => [:get,:post,:put]
If you just want to use the bar
method given the condition being met 如果您只想在满足条件的情况下使用
bar
方法
def foo
if 'XYZ'
bar
else
render 'view'
end
end
This would run the bar
method and make use of any redirect you have in there. 这将运行
bar
方法,并利用其中的任何重定向。 It is more likely that you want to reexamine what it is you are trying to achieve. 您更有可能想重新检查您要达到的目标。
If you can give more specifics then you will get a better answer. 如果您可以提供更多详细信息,那么您将获得更好的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.