2010-11-06 78 views
2

在我的Ruby on Rails应用程序中Rails显示来自不同控制器的视图

我有一个页面关于 - 有一个控制器和视图。没有模型。

我有一个评论模型(由轨道生成生成脚手架评论) - 这有一个控制器,视图,模型

在我的公司简介页面上,我想显示从注释模型“注释”,所以我正在考虑在我的“简介”页面上使用评论索引操作(列出评论)和新操作(以创建新评论)。

我很难得到这个权利。

这是我做过什么: 公司简介控制器,我加入
redirect_to :controller => "comments", :action => "index"

公司简介的意见,我说
<%= render 'comments/index' %>

它不工作,给了我未定义redirect_to的和无对象@comments错误。 您能否告诉我
1.适当的方法做到这一点
2.语法
3.任何事情要做的config.rb?

回答

3

你想创建一个部分,你用它来渲染在评论索引视图的意见,而且在您的视图的公司简介页面

# in about_us and in 'comments#index' 
<%= render :partial 'path/to/_partial' %> 

#in the about_us controller, or whatever controller dispatches the about us view 

@comments = Comment.all.where(:my_conditions) 

#partial view 

<% @comments.each do |comment| %> 
.. 
<% end %> 
+0

这一工程非常感谢。 – 2010-11-06 18:22:53

+0

不应该是Comment.where(:my_conditions)吗?当在视图中调用每个时,它将执行查询。 – 2010-11-07 00:44:49

+0

好点。我忘了Rails 3和延迟加载集合。 – 2010-11-10 13:08:55

相关问题