Ruby-on-rails 从另一个控制器渲染控制器动作
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3370234/
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
Render controller action from another controller
提问by user142913
I think the code is more explicit
我认为代码更明确
option A
选项A
class RedirectController < ApplicationController
def index
redirect_to :controller => 'posts', :action => 'show', :id => 1
# it works
end
end
option B
选项 B
class RedirectController < ApplicationController
def index
render :controller => 'posts', :action => 'show', :id => 1
# it doesn't work
end
end
Is possible in (B) to load another action in another controller? (and not just the view) How? Thanks
在(B)中是否可以在另一个控制器中加载另一个动作?(而不仅仅是视图)如何?谢谢
回答by Bohdan
Try render 'posts/show'or render :template => 'posts/show'
尝试render 'posts/show'或render :template => 'posts/show'
回答by Deepak Mahakale
Just render the template
只需渲染模板
def index
render 'posts/show'
end
This one also works
这个也有效
def index
render template: 'posts/show'
end
If you want to render in some other layout
如果你想在其他布局中渲染
def index
render template: 'posts/show', layout: 'different_layout'
end

