Rails 3 从另一个控制器渲染动作
我需要渲染另一个控制器操作 <%= render "controller/index" %>
我收到这个错误
缺少部分控制器/索引 {:formats=>[:html], :locale=>[:en, :en], :handlers=>[:rjs, :rhtml, :rxml, :erb , :builder]} 在视图路径“/path_to/app/views”
如何将另一个控制器操作渲染到视图中而不向客户端发送重定向? 我已经尝试过
<%=render :action => "index", :controller=>"controller" %>
,但似乎不起作用。
I need to render another controller action <%= render "controller/index" %>
and i get this error
Missing partial controller/index with {:formats=>[:html], :locale=>[:en, :en], :handlers=>[:rjs, :rhtml, :rxml, :erb, :builder]} in view paths "/path_to/app/views"
how can i render another controller action into a view but without sending an redirect to the client ?
I've tried
<%=render :action => "index", :controller=>"controller" %>
but it seems that is not working.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试渲染 template:
或 file:
我相信你应该通过控制器渲染它,因为它更方便:
Try to render template:
Or file:
And I believe you should render it through controller, as far as it is more convenient:
这对我来说效果很好:
然后,基本上调用
它会破解另一个类并覆盖其“params”方法并返回
如果您使用的是Rails 4:
This works well for me :
then, call by
basically it hacks the other class and overwrites its "params" method and return
If you are using Rails 4:
来自 Rails 指南页面:
简而言之,您无法渲染另一个动作,只能渲染另一个模板。您可以获取共享代码并将其移动到应用程序控制器中的方法。如果您确实无法以其他方式构建代码,您也可以尝试以下方法:
From Rails Guides page:
So in short you can't render another action, you can only render another template. You could get the shared code and move it to a method in application controller. You could also try something along this lines if you really can't structure your code in some other way:
如果您不想只渲染另一个控制器(/model)的
view
,而是调用action
(方法),请更多地考虑Ruby的生活方式 - put将此方法放入模块
中,并将其包含在您需要的控制器中。我认为它不像触摸其他控制器那样“诡异”。
If you do not want just to render the
view
of the other controller (/model), but calling theaction
(method), think more the ruby way of life - put this method(s) into amodule
and include it in the controllers you need it.I think its less 'spooky' then somehow touching an other controller.
渲染另一个控制器的部分内容是没有问题的。
那么为什么不将原始操作视图移动到局部视图并在需要的地方渲染此局部视图:
将 app/views/controller_a/index.html.erb 移动到
app/views/controller_a/_index.html.erb 。
应用程序/视图/controller_a/index.html.erb:
应用程序/视图/controller_b/some_action.html.erb:
Rendering partials of another controller is no problem.
So why not moving the original action view to a partial and render this partial wherever you need it:
move app/views/controller_a/index.html.erb to
app/views/controller_a/_index.html.erb .
app/views/controller_a/index.html.erb:
app/view/controller_b/some_action.html.erb: