Rails rails4 与 rails3 的 render

ane · July 02, 2014 · Last by qifengle replied at July 02, 2014 · 2110 hits

在 rails4 中,如果想要在页面循环 notices。只需要写成 <%= render @notices %>

然后再写一个_notice.html.erb

<li>
    <%= link_to notice.title[0,11], notice.url, :target => "_blank" %>
</li>

这样就大功告成了,印象中好像是因为 rails4 的 render 有个:html 选项。

今天在 rails3 中,使用,失败了。于是改用 help 的方式

def iteration_notices(notices)
   notices.each do |notice| 
     render :partial=>'notice', :obj => notice
   end
 end

obj 也按照正确的对象渲染了 partial.可最总的页面居然现实的是 obj 对象字符串。不是很理解

render partial: 'notice', collection: @notices

#1 楼 @hz_qiuyuanxin 这个好像是 rails2 的,rails3 不支持。而且你用的是 rails4 的写法

#2 楼 @ane 1 楼的做法是支持的,文档中有说明哦。。http://guides.rubyonrails.org/v3.2.18/layouts_and_rendering.html#using-partials

一般都是因为没有按照约定来,实在不行,再加个 as 的参数塞。

#3 楼 @qifengle 那估计这个 collectons 必须是一个 model 才行,随便一个集合,估计不行

#4 楼 @ane 我一直这样用的:

<%=render :partial=>'reply',  :collection => @replies, :as => :reply%>

其实貌似如果 as 作为一个标准的单数形式,貌似可以不用写阿。

#5 楼 @qifengle 你的 replies 是个继承了 ActiceRecord 的一个集合吧。我的是一个 mongo 的集合

You need to Sign in before reply, if you don't have an account, please Sign up first.