简体   繁体   English

Rails:从一个控制器动作重定向到另一个(非GET)

[英]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.

相关问题 从一个控制器调用Rails 5动作 - Calling a Rails 5 action from one controller to another 从一个控制器重定向到另一个控制器时,Rails 4:redirect_to错误 - Rails 4: redirect_to error when redirecting from one controller to another 将一个控制器动作映射到另一个动作导轨上 - Map one controller action on another action rails 重定向到Rails中的另一个控制器 - Redirecting to another controller in Rails 在Rails中从一个应用程序到另一个应用程序调用控制器操作 - Call controller action from one application to another in Rails 在RAILS中将模型从一个控制器动作传递到另一个控制器动作的替代方法 - Alternative ways to pass model from one controller action to another in RAILS Rails 3.2 将参数从一个控制器动作传递到另一个控制器动作 - Rails 3.2 passing parameters from one controller action to another Rails:如何在同一个控制器中将值从一个动作传递到另一个动作 - Rails: How to pass value from one action to another in the same controller Rails:从控制器调用另一个控制器动作 - Rails: call another controller action from a controller Rails - 如何避免必须从另一个控制器中的create action触发一个控制器中的create操作 - Rails - how do I avoid having to trigger the create action in one controller from the create action in another controller
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM