Ruby-on-rails 渲染部分 :object 与 :locals

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5122223/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 00:31:02  来源:igfitidea点击:

render partial :object vs :locals

ruby-on-rails

提问by Chris Muench

<%= render :partial => 'partial/path', :locals => {:xyz => 'abc'} %>

vs

对比

<%= render :partial => 'partial/path', :object => @some_object %>

I think the first one make a local variable named xyzavailable in the partial and the second one makes a local variable named objectavailable in the partial. So what is the difference? (Besides localsallows more than one variable)

我认为第一个使名为局部变量的局部变量xyz在部分中可用,第二个使名为局部变量的局部变量object在局部中可用。那么区别是什么呢?(除了locals允许多个变量)

采纳答案by Wizard of Ogz

In the second case using :object will define a variable with the same name as the partial by default. If my partial template is named _user.html.erb then there will be a local variable named "user" defined in the template.

在第二种情况下,使用 :object 将默认定义一个与部分名称相同的变量。如果我的部分模板名为 _user.html.erb,那么模板中将定义一个名为“user”的局部变量。

You can specify a different variable name with :as => "another_name".

您可以使用 :as => "another_name" 指定不同的变量名称。

This is documented here: http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html, here: http://apidock.com/rails/ActionView/PartialRenderer

此处记录:http: //api.rubyonrails.org/classes/ActionView/PartialRenderer.html,此处:http: //apidock.com/rails/ActionView/PartialRenderer

...and for older Rails (version <= v3.09): http://apidock.com/rails/ActionView/Partials

...对于较旧的 Rails(版本 <= v3.09):http://apidock.com/rails/ActionView/Partials

回答by nathanvda

The second form

第二种形式

render :partial => 'account', :object => @some_account

will make sure the accountvariable in the partial will be set to @some_account. You can rename the variable using the :asoption.

将确保account部分中的变量将设置为@some_account. 您可以使用该:as选项重命名变量。

The biggest advantage of the :localsis that

最大的优点:locals就是

  • you have very clear control over the objects and names
  • you can assign more than 1 variable
  • 您可以非常清楚地控制对象和名称
  • 您可以分配 1 个以上的变量

So you could do something like

所以你可以做类似的事情

render partial => 'some_view', :locals => { :user => account.user, :details => some_details_we_retrieved }

making a clear seperation possible when needed.

在需要时进行明确的分离。

The disadvantage of the :localsapproach is that it is more verbose, and sometimes a simple

这种:locals方法的缺点是比较冗长,有时也很简单

render :partial => 'account'

is identical to

等同于

render :partial => 'account', :locals => {:account => @account }

So use the one which suits you the best (or where it suits the best).

因此,请使用最适合您的(或最适合您的)。

回答by Ryan

If you're using Rails 3+, there's an even easier syntax to use:

如果您使用的是 Rails 3+,则可以使用更简单的语法:

# Instead of <%= render partial: "account", locals: { account: @buyer } %>
<%= render 'account', account: @buyer %>

Source: Action View Partials

来源:Action View Partials