Ruby-on-rails 一个控制器使用另一个控制器的视图进行渲染
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1013152/
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
One controller rendering using another controller's views
提问by Paul
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.
我有 QuestionController 我现在有 AnotherQuestionController 其动作应该使用 app/views/question/ 中的模板和部分呈现这可能吗?好像应该是这样。
I've tried
我试过了
render :template => "question/answer"
but answer.html.erb includes partials and I get errors like
但 answer.html.erb 包含部分,我得到了类似的错误
"Missing template another_question/_my_partial.erb in view path"
“视图路径中缺少模板 another_question/_my_partial.erb”
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).
那么有没有办法告诉Rails“将AnotherQuestionController视为它的QuestionController并在app/views/question中查找视图和部分”?或者我是否必须创建 app/views/another_question - 这会导致重复(这不能是 Rails 的方式)。
Thanks
谢谢
回答by Ben Hughes
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
您遇到的问题来自于在错误位置寻找的部分。修复很简单,只需在任何共享模板中使您的部分成为绝对。例如, question/answer.html.erb 应该有
<%= render :partial => 'question/some_partial' %>
rather than the usual
而不是通常的
<%= render :partial => 'some_partial' %>
回答by klew
You can achieve it with:
您可以通过以下方式实现:
render 'question/answer'
回答by Steve
Rails uses a list of prefixes to resolve templates and partials. While you canexplicitly specify a prefix ("question/answer"), as suggested in another answer, this approach will fail if the template itself includes unqualified references to other partials.
Rails 使用前缀列表来解析模板和部分。虽然您可以按照另一个答案中的建议明确指定前缀(“问题/答案”),但如果模板本身包含对其他部分的非限定引用,则此方法将失败。
Assuming that you have an ApplicationController superclass, and QuestionController inherits from it, then the places Rails would look for templates are, in order, "app/views/question/" and "app/views/application/". (Actually it will also look in a series of view paths, too, but I'm glossing over that for simplicity's sake.)
假设您有一个 ApplicationController 超类,并且 QuestionController 继承自它,那么 Rails 查找模板的位置依次为“app/views/question/”和“app/views/application/”。(实际上,它也会查看一系列视图路径,但为了简单起见,我将其忽略。)
Given the following:
鉴于以下情况:
class QuestionController < ApplicationController
end
class AnotherQuestionController < ApplicationController
end
QuestionController._prefixes
# => ["question", "application"]
AnotherQuestionController._prefixes
# => ["another_question", "application"]
Solution #1.Place the partial under "app/views/application/" instead of "app/views/question/", where it will be available to both controllers.
解决方案#1。将部分放在“app/views/application/”下,而不是“app/views/question/”下,两个控制器都可以使用它。
Solution #2.Inherit from QuestionController, if appropriate.
解决方案#2。如果合适,从 QuestionController 继承。
class AnotherQuestionController < QuestionController
end
=> nil
AnotherQuestionController._prefixes
# => ["another_question", "question", "application"]
Solution #3.Define the class method AnotherQuestionController::local_prefixes
解决方案#3。定义类方法 AnotherQuestionController:: local_prefixes
This was added in Rails 4.2.
这是在 Rails 4.2 中添加的。
class AnotherQuestionController < ApplicationController
def self.local_prefixes
super + ['question']
end
end
AnotherQuestionController._prefixes
# => ["another_question", "question", "application"]
回答by Shadwell
You could try the inherit_views plugin (http://github.com/ianwhite/inherit_views/tree/master) I mentioned here in the answer to this question.
你可以试试inherit_views插件(http://github.com/ianwhite/inherit_views/tree/master)我在这个问题的回答中提到的。

