1. Partial
1.1 Use partial as a part of the view for rendering. You can call the render method:
<% = Render: Partial => "menu" %>
The aboveCodeThe template named _menu.html. ERB is rendered to the current template.
<% = Render: Partial => "shared/menu" %>
Render APP/views/shared/_menu.html. ERB to the current template.
1.2 layout can be specified separately for partial:
<% = Render: Partial => "link_area",: Layout => "graybar" %>
The layout file name of partial must start with an underscore: _graybar.html. ERB, and the layout template file and partial must be placed in the same directory.
2. Pass local variables to partial
2.1: locals option is used to set local variables of partial:
<% = Render: Partial => "form",: locals =>{: button_label => "create zone",: Zone =>@ zone }%>
In this example, the variables button_label and zone can be accessed in _form.html. ERB.
2.2 Each partial has a local variable with the same name as partial (without underline). You can pass the value to this variable through the: object option:
<% = Render: Partial => "customer",: Object =>@ new_customer %>
In this example, you can access the customer variable in _customer.html. ERB, which points to @ new_customer.
Of course, as part of the parent template (parent), partial can directly access the instance variables of the parent template, such as @ new_customer here, but if so, partial is coupled with the parent template, making it difficult to reuse it. Therefore, we recommend that you use the partial name to reference instance variables instead of directly accessing instance variables.
In earlier versions of rails, if the: object or: locals option is not specified, rails will automatically find the instance variable with the same name as partial in the parent template as a local variable of partial, for example:
<% = Render: Partial => "customer" %>
If you access the customer variable in _customer.html. ERB, rails will automatically search for the instance variable named @ customer in the parent template. Deprecated is not recommended for this feature in rails2.2 ). Rails3.0 has removed this feature.
2.3 if you want to pass the instance variable = partial name = model name to partial, You Can abbreviated it:
# When @ customer is the instance of the model "customer" and partial is named "customer"
<% = Render: Partial => @ customer %>
# Equivalent
<% = Render: Partial => "customer",: Object =>@ customer %>
3. Rendering set (collections)
3.1: The collection option is used to specify the collection object that is passed to partial.
Suppose there is a collection like books, which contains five book objects. You can use it like this:
Using main.html. ERB
<% = Render: Partial => "book",: Collection => books %>
Performance_book.html. ERB
<P> <% = book. name %> </P>
In this example, the content of _book.html. ERB is rendered five times in main.html.erb. In this case, the variable with the same name as partial in the partial template points to each item in the collection passed by the collection option. If you do not want to use the variable name with the same name as partial, you can set the name of the variable you want through the: As option (the value of as can only use symbol, not string, otherwise, the nil value will be obtained in partial ):
<% = Render: Partial => "product",: Collection =>@ products,: As =>: item %>
3.2 subscript index value
When setting: collection options, rails also provides a counter variable to the partial template. The variable name starts with partial (not underlined) and ends with _ counter, this variable name is not affected by the as option (that is, in the code above, this variable name should be product_counter rather than item_counter ). The value is the index value of the collection object (starting from 0 ).
3.3: spacer_template.eu-central-1.maxcompute.aliyun-inc.com/api
: The spacer_template option is used to specify the template filled in between each member of the collection:
<% = Render: Partial => "product",: Collection => @ products,: spacer_template => "product_rtemplate" %>
In the lifecycle code, the content of _product_ruler.html. ERB will be filled into every pair of _ product partial.
Like: object,: Collection also has the abbreviated form: <% = render: Partial => @ products %>