1. 預設參數
Ruby程式碼
<%= render :partial => "account" %>
預設本地有個變數@account, 傳遞過去,render到的partial(_account.erb)有個變數個轉號組hash參數hash 值是本地的變量,hash的key是partial裡的變量
Ruby代碼
<%= render :partial => "account", :locals => { :account => @buyer } %> <% for ad in @advertisements %> <%= render :partial => "ad", :locals => { :ad => ad } %> <% end %>
上面兩個render
傳遞本地變量@buyer到_account.erb裡。傳遞本地變量ad到_ad.erb裡叫ad
3
根據1默認參數下面兩個是一樣的
Ruby代碼
<%= render :partial => "contract", :locals => { :contract => @contract } %> <%= render :partial => "contract" %>
用來改變傳遞之後,partial裡變數的名稱,如下,render方式是一樣的。問
所以什麼都記不清楚的時候,就用object多寫點都能表達
如下:
Ruby程式碼
<%= render :partial => "contract", :as => :agreement <%= render :partial => "contract", :locals => { :agreement => @contract }
6 object和as合用
Ruby程式碼
<%= render :partial => "account", :object => @buyer %> <% for ad in @advertisements %> <%= render :partial => "ad", :object => ad %> <% end %>
<%= render :partial => "contract", :object => @contract, :as => :contract %> <%= render :partial => "contract" %>
@advertisements是一個array,partial裡_ad.erb有個ad變數是@advertisements的成員。就是_ad.erb不用寫loop只是一條廣告顯示。
Ruby程式碼
<%= render :partial => "ad", :collection => @advertisements %>
<%= render :partial => "ad", :collection => @advertisements, :spacer_template => "ad_divider" %>
#@account是一条记录 # <%= render :partial => "accounts/account", :locals => { :account => @account} %> <%= render :partial => @account %> # @posts是一组记录 # <%= render :partial => "posts/post", :collection => @posts %> <%= render :partial => @posts %>
Ruby代碼
#<%= render :partial => "account" %>可用下面代替 <%= render "account" %> #<%= render :partial => "account", :locals => { :account => @buyer } %>可用下面代替 <%= render "account", :account => @buyer %> # @account是一条记录 # <%= render :partial => "accounts/account", :locals => { :account => @account } %>可用下面代替 <%= render(@account) %> # @posts是一组记录 # <%= render :partial => "posts/post", :collection => @posts %>可用下面代替 <%= render(@posts) %>