Home  >  Article  >  php教程  >  rails render partial parameter variable description

rails render partial parameter variable description

高洛峰
高洛峰Original
2016-12-13 11:54:121554browse

1. Default parameters

Ruby code

<%= render :partial => "account" %>


By default, there is a local variable @account, which is passed to the partial (_account.erb) rendered to has a variable account

2. Separate parameters locals
locals is passed The hash value of the group hash parameter is a local variable, and the hash key is a variable in the partial

Ruby code

<%= render :partial => "account", :locals => { :account => @buyer } %>  
  
<% for ad in @advertisements %>  
  <%= render :partial => "ad", :locals => { :ad => ad } %>  
<% end %>


The two renders above are
pass the local variable @buyer to the parameter called account in _account.erb
Pass the local variable ad to _ad.erb and call it ad

3

According to 1 default parameter, the following two are the same

Ruby code

<%= render :partial => "contract", :locals => { :contract  => @contract } %>  
  
<%= render :partial => "contract" %>




4. as use

to change after passing , the name of the variable in the partial is as follows, and the rendering method is the same.

Ruby code

<%= render :partial => "contract", :as => :agreement  
  
<%= render :partial => "contract", :locals => { :agreement => @contract }


Pass @contract to _contract.erb, the variable in partial is called agreement

5. The easiest way to use object

object is to pass the original name of a variable to partial,
So when you can’t remember anything clearly, just use object and write more to express it

as follows:

Ruby code

<%= render :partial => "account", :object => @buyer %>  
  
<% for ad in @advertisements %>  
  <%= render :partial => "ad", :object => ad %>  
<% end %>


Pass @buyer to the variable name in the partial of _account.erb or @buyer
Pass ad The variable name in the partial of _ad.erb is still ad

6 object and as are combined

Ruby code

<%= render :partial => "contract", :object => @contract, :as => :contract %>  
  
<%= render :partial => "contract" %>



7 collection

Ruby code

<%= render :partial => "ad", :collection => @advertisements %>



@advertisements is an array, partial There is an ad variable in _ad.erb which is a member of @advertisements. That is, _ad.erb does not need to write loop, it is just an advertisement display.

Ruby code

<%= render :partial => "ad", :collection => @advertisements, :spacer_template => "ad_divider" %>


Same as above, loop to display _ad.erb _ad_divider.erb to display @advertisements.size times where partial_ad_counter is the default counter indicating which advertisement

8 default

depends on the variable you want to be partial. A group of records is still a record, and locals and collections will be used according to the agreement

Ruby code

#@account是一条记录  
# <%= render :partial => "accounts/account", :locals => { :account => @account} %>  
<%= render :partial => @account %>  
  
# @posts是一组记录  
# <%= render :partial => "posts/post", :collection => @posts %>  
<%= render :partial => @posts %>


This is a very human agreement, but sometimes we just forget

9 some beautiful abbreviations

Ruby code

#<%= 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) %>



10 layout

Ruby code

<%# app/views/users/index.html.erb &>  
Here&#39;s the administrator:  
<%= render :partial => "user", :layout => "administrator", :locals => { :user => administrator } %>  
  
Here&#39;s the editor:  
<%= render :partial => "user", :layout => "editor", :locals => { :user => editor } %>  
  
<%# app/views/users/_user.html.erb &>  
Name: <%= user.name %>  
  
<%# app/views/users/_administrator.html.erb &>  
<div id="administrator">  
  Budget: $<%= user.budget %>  
  <%= yield %>  
</div>  
  
<%# app/views/users/_editor.html.erb &>  
<div id="editor">  
  Deadline: <%= user.deadline %>  
  <%= yield %>  
</div>



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Linux reboot commandNext article:Linux reboot command