在rails中渲染的:locals是什么?

use*_*363 3 ruby-on-rails render

这是渲染的API定义:

render(options = {}, locals = {}, &block)

Returns the result of a render that’s dictated by the options hash. The primary options are:

    :partial - See ActionView::Partials.

    :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.

    :text - Renders the text passed in out.
Run Code Online (Sandbox Code Playgroud)

这里没有关于当地人的目的的解释?当地人干什么?

谢谢。

Dav*_*ton 7

将局部变量传递给部分模板,而不是控制器实例变量。

请参见《布局和渲染指南》中的第3.4.4节“传递局部变量”。


Kuo*_*mmy 4

例如:

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

    这意味着已经有一个@account为部分调用的实例变量,并且您将其传递给部分。

  2. <%= render :partial => "account", :locals => { :account => @buyer } %>

    这意味着您将调用的本地实例变量传递@buyeraccount部分,并且部分中的变量account称为@account。即,哈希{ :account => @buyer }:locals用于将局部变量传递给部分变量。as您也可以以相同的方式使用关键字:

    <%= render :partial => "contract", :as => :agreement

    与以下内容相同:

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