一个控制器使用另一个控制器的视图呈现 [英] One controller rendering using another controller's views
问题描述
我有QuestionController
我现在有AnotherQuestionController的动作应该使用模板和局部应用程序中应用程序/ views /问题/
这是可能的吗?似乎应该是。
我试过了
:template => question / answer
但answer.html.erb包含partials, >
在视图路径中缺少模板another_question / _my_partial.erb
因此有一种方法可以告诉Rails处理AnotherQuestionController好像它的QuestionController,并在app / views /问题中查找视图和局部视图?
或者我将创建app / views / another_question - 这将导致重复(这不能是Rails的方式)。
感谢
模板渲染实际上应该工作
render:template => question / answer
你所遇到的问题是从错误的地方看。修复是简单的,只是使你的部分在任何共享模板中是绝对的。例如,question / answer.html.erb应该有
<%= render:partial => 'question / some_partial'%>
而不是通常
<%= render:partial => 'some_partial'%>
I have QuestionController I now have AnotherQuestionController with actions which should render using templates and partials in app/views/question/ Is this possible? Seems like it should be.
I've tried
render :template => "question/answer"
but answer.html.erb includes partials and I get errors like
"Missing template another_question/_my_partial.erb in view path"
So is there a way to tell Rails "treat AnotherQuestionController as if its QuestionController and look for views and partials in app/views/question"? Or will I have to create app/views/another_question - which will cause duplication (this can't be the Rails way).
Thanks
Template rendering should actually work
render :template => "question/answer"
The problem you were having is from the partials looking in the wrong place. The fix is simple, just make your partials absolute in any shared templates. For example, question/answer.html.erb should have
<%= render :partial => 'question/some_partial' %>
rather than the usual
<%= render :partial => 'some_partial' %>
这篇关于一个控制器使用另一个控制器的视图呈现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!