Ruby-on-rails Rails:从控制器调用另一个控制器动作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5767222/
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
Rails: call another controller action from a controller
提问by ddayan
I need to call the create action in controller A, from controller B.
我需要从控制器 B 调用控制器 A 中的创建操作。
The reason is that I need to redirect differently when I'm calling from controller B.
原因是当我从控制器 B 调用时,我需要以不同的方式重定向。
Can it be done in Rails?
可以在 Rails 中完成吗?
采纳答案by Spyros
You can use a redirect to that action :
您可以使用重定向到该操作:
redirect_to your_controller_action_url
More on : Rails Guide
更多关于:Rails 指南
To just render the new action :
仅呈现新动作:
redirect_to your_controller_action_url and return
回答by Sammy Larbi
To use one controller from another, do this:
要从另一个控制器使用一个控制器,请执行以下操作:
def action_that_calls_one_from_another_controller
controller_you_want = ControllerYouWant.new
controller_you_want.request = request
controller_you_want.response = response
controller_you_want.action_you_want
end
回答by apneadiving
The logic you present is not MVC, then not Rails, compatible.
您提出的逻辑不兼容 MVC,也不兼容 Rails。
A controller renders a view or redirect
A method executes code
控制器呈现视图或重定向
一个方法执行代码
From these considerations, I advise you to create methods in your controller and call them from your action.
基于这些考虑,我建议您在控制器中创建方法并从您的操作中调用它们。
Example:
例子:
def index
get_variable
end
private
def get_variable
@var = Var.all
end
That said you can do exactly the same through different controllers and summon a methodfrom controller A while you are in controller B.
也就是说,您可以通过不同的控制器执行完全相同的操作,并在您在控制器 B 中时从控制器 A调用方法。
Vocabulary is extremely important that's why I insist much.
词汇非常重要,这就是为什么我坚持很多。
回答by austin
You can use url_forto get the URL for a controller and action and then use redirect_toto go to that URL.
您可以使用url_for获取控制器和操作的 URL,然后使用redirect_to转到该 URL。
redirect_to url_for(:controller => :controller_name, :action => :action_name)
回答by fl00r
This is bad practice to call another controller action.
调用另一个控制器操作是不好的做法。
You should
你应该
- duplicate this action in your controller B, or
- wrap it as a model method, that will be shared to all controllers, or
- you can extend this action in controller A.
- 在您的控制器 B 中复制此操作,或
- 将其包装为模型方法,该方法将共享给所有控制器,或者
- 您可以在控制器 A 中扩展此操作。
My opinion:
我的看法:
- First approach is not DRY but it is still better than calling for another action.
- Second approach is good and flexible.
Third approach is what I used to do often. So I'll show little example.
def create @my_obj = MyModel.new(params[:my_model]) if @my_obj.save redirect_to params[:redirect_to] || some_default_path end end
- 第一种方法不是 DRY,但它仍然比要求另一个行动要好。
- 第二种方法很好而且很灵活。
第三种方法是我以前经常做的。所以我会展示一个小例子。
def create @my_obj = MyModel.new(params[:my_model]) if @my_obj.save redirect_to params[:redirect_to] || some_default_path end end
So you can send to this action redirect_toparam, which can be any path you want.
所以你可以发送到这个动作redirect_to参数,它可以是你想要的任何路径。
回答by Michael Durrant
Perhaps the logic could be extracted into a helper? helpers are available to all classes and don't transfer control. You could check within it, perhaps for controller name, to see how it was called.
也许逻辑可以提取到助手中?帮助程序可用于所有类,并且不转移控制权。您可以在其中检查,也许是控制器名称,以查看它是如何调用的。
回答by Oleg Afanasyev
Compositionto the rescue!
组成来拯救!
Given the reason, rather than invoking actions across controllers one should design controllers to seperate shared and custom parts of the code. This will help to avoid both - code duplication and breaking MVC pattern.
鉴于原因,与其在控制器之间调用操作,不如将控制器设计为将代码的共享部分和自定义部分分开。这将有助于避免代码重复和破坏 MVC 模式。
Although that can be done in a number of ways, using concerns (composition) is a good practice.
尽管这可以通过多种方式完成,但使用关注点(组合)是一种很好的做法。
# controllers/a_controller.rb
class AController < ApplicationController
include Createable
private def redirect_url
'one/url'
end
end
# controllers/b_controller.rb
class BController < ApplicationController
include Createable
private def redirect_url
'another/url'
end
end
# controllers/concerns/createable.rb
module Createable
def create
do_usefull_things
redirect_to redirect_url
end
end
Hope that helps.
希望有帮助。
回答by CodecPM
You can call another action inside a action as follows:
你可以在一个动作中调用另一个动作,如下所示:
redirect_to action: 'action_name'
重定向到动作:'action_name'
class MyController < ApplicationController
def action1
redirect_to action: 'action2'
end
def action2
end
end
回答by Michale.Gaocl
Separate these functions from controllers and put them into model file. Then include the model file in your controller.
将这些功能从控制器中分离出来并放入模型文件中。然后将模型文件包含在您的控制器中。

