一个控制器使用另一个控制器的视图进行渲染 [英] One controller rendering using another controller's views

查看:20
本文介绍了一个控制器使用另一个控制器的视图进行渲染的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有 QuestionController我现在有另一个问题控制器,其动作应该使用 app/views/question/中的模板和部分呈现这可能吗?好像应该是的.

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.

我试过了

render :template => "question/answer"

但 answer.html.erb 包含部分,我得到像

but answer.html.erb includes partials and I get errors like

视图路径中缺少模板 another_question/_my_partial.erb"

"Missing template another_question/_my_partial.erb in view path"

那么有没有办法告诉Rails将AnotherQuestionController当作它的QuestionController并在app/views/question中查找视图和部分"?或者我是否必须创建 app/views/another_question - 这会导致重复(这不是 Rails 的方式).

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).

谢谢

推荐答案

模板渲染应该可以正常工作

Template rendering should actually work

 render :template => "question/answer"

您遇到的问题来自于在错误位置寻找的部分.修复很简单,只需在任何共享模板中使您的部分成为绝对.例如,question/answer.html.erb 应该有

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' %>

而不是平常

<%= render :partial => 'some_partial' %> 

这篇关于一个控制器使用另一个控制器的视图进行渲染的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆