表单: RailsNotes
用户: dreamable
创建日期: 2021-03-01 23:07:23 UTC
更新日期: 2021-03-01 23:51:01 UTC
引用:(Table ID 3, Record ID 27)

标题 :
Rails Render
笔记 :
render(options = {}, locals = {}, &block)

The primary options are:
    :partial - See ActionView::PartialRenderer.
    :file - Renders an explicit template file (this used to be the old default), add :locals to pass in those.
    :inline - Renders an inline template similar to how it's done in the controller.
    :plain - Renders the text passed in out. Setting the content type as text/plain.
    :html - Renders the HTML safe string passed in out, otherwise performs HTML escape on the string first. Setting the content type as text/html.
    :body - Renders the text passed in, and inherits the content type of text/plain from ActionDispatch::Response object.
  • Specify layout

    <%= render partial: "link_area", layout: "graybar" %>
    
  • Block

    # users/index.html.erb
    <%= render "shared/search_filters", search: @q do |form| %>
    <p>
    Name contains: <%= form.text_field :name_contains %>
    </p>
    <% end %>
    #
    # shared/_search_filters.html.erb
    <%= form_with model: search do |form| %>
    <h1>Search form:</h1>
    <fieldset>
    <%= yield form %>
    </fieldset>
    <p>
    <%= form.submit "Search" %>
    </p>
    <% end %>
    
  • Passing local variables

    <%= render partial: "form", locals: {zone: @zone} %>
    
  • :as and :object options
    By default, PartialRenderer uses the template name for the local name of the object passed into the template. These examples are effectively the same:

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

    By specifying the :as option we can change the way the local variable is named in the template. These examples are effectively the same:

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

    The :object option can be used to directly specify which object is rendered into the partial.

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

    The :object and :as options can be used together. We might have a partial which we have named genericly, such as ‘form’. Using :object and :as together helps us.

    <%= render :partial => "form", :object => @contract, :as => :contract %>
    <%= render :partial => "form",  :locals => {:contract => @contract} %> 
    
  • Collection

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

    Also, you can specify a partial which will be render as a spacer between each element by passing partial name to :spacer_template.

    <%= render :partial => "ad", :collection => @ads, :spacer_template => "ad_divider" %>
    
  • Rendering the default case
    Instead of explicitly naming the location of a partial, you can also let the RecordIdentifier do the work if you’re following its conventions for RecordIdentifier#partial_path.

    # @account is an Account instance, so it uses the RecordIdentifier to replace
    # <%= render :partial => "accounts/account", :locals => { :account => @account} %>
    <%= render :partial => @account %>
    # @posts is an array of Post instances, so it uses the RecordIdentifier to replace
    # <%= render :partial => "posts/post", :collection => @posts %>
    <%= render :partial => @posts %>
    

    If you’re not going to be using any of the options like collections or layouts, you can also use the short-hand defaults of render to render partials. Examples:

    # Instead of <%= render :partial => "account" %>
    <%= render "account" %>
    # Instead of <%= render :partial => "account", :locals => { :account => @buyer } %>
    <%= render "account", :account => @buyer %>
    # @account is an Account instance, so it uses the RecordIdentifier to replace
    # <%= render :partial => "accounts/account", :locals => { :account => @account } %>
    <%= render(@account) %>
    # @posts is an array of Post instances, so it uses the RecordIdentifier to replace
    # <%= render :partial => "posts/post", :collection => @posts %>
    <%= render(@posts) %>
    

    Question: some place says render will not accept additional local variables for the partial. But it seems I can pass multiple variables for render. New feature (Rails 6)?

    <%= render 'record', table: @table, record: @record %>
    
标签: