我有 QuestionController 我现在有 AnotherQuestionController ,其操作应该使用 app/views/question/中的模板和部分进行渲染 这可能吗?看起来应该是这样。
我已经尝试过
render :template => "question/answer"
但是answer.html.erb 包含部分内容,我收到类似
的错误“ View 路径中缺少模板 another_question/_my_partial.erb”
那么有没有办法告诉 Rails“将 AnotherQuestionController 视为其 QuestionController 并在 app/views/question 中查找 View 和部分”? 或者我是否必须创建 app/views/another_question - 这将导致重复(这不能是 Rails 方式)。
谢谢
最佳答案
模板渲染应该确实有效
render :template => "question/answer"
您遇到的问题是部分内容查找到了错误的位置。修复方法很简单,只需在任何共享模板中将您的部分设置为绝对即可。例如,question/answer.html.erb 应该有
<%= render :partial => 'question/some_partial' %>
与平常不同
<%= render :partial => 'some_partial' %>
关于ruby-on-rails - 一个 Controller 使用另一个 Controller 的 View 进行渲染,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1013152/