2008年4月12日 星期六

Partial 怎麼用, example

[controller]
def show
@users = User.find(:all)
end

[view, show.rhtml]
用:object是 @users 單純傳入 _objects.rhtml 裡, 而在partial就叫做 objects
<%= render :partial => "objects", :object => @users %>


[view, _objects.rhtml] collection = @user.each 被拆成一個個後傳入
用:collection 是 object 被一個個拆開後才丟入 _collection.rhtml, 而在裡面被拆開後的local variable就叫做 collection
<%= render :partial => "collection", :collection => objects%>

[view, _collection.rhtml], _collection 就像是已經有個迴圈包起來一樣
<%= collection.name %>
<%= collection.address %>


<%= render :partial =>"collection", :collection => objects %>

<% objects.each do |e| %>
<%= e.name%>
<%= e.address %>
<% end %>
結果一樣只是連迴圈都省了

沒有留言: