search
Homephp教程PHP开发rails render partial parameter variable description

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!