渲染部分 :object 与 :locals [英] render partial :object vs :locals
问题描述
<%= render :partial => 'partial/path', :locals => {:xyz => 'abc'} %>
对比
<%= render :partial => 'partial/path', :object => @some_object %>
我认为第一个使名为 xyz
的局部变量在局部变量中可用,第二个使名为 object
的局部变量在局部变量中可用.那么区别是什么呢?(除了locals
允许多个变量)
I think the first one make a local variable named xyz
available in the partial and the second one makes a local variable named object
available in the partial. So what is the difference? (Besides locals
allows more than one variable)
推荐答案
在第二种情况下,使用 :object 将默认定义一个与部分名称相同的变量.如果我的部分模板名为 _user.html.erb,那么模板中将定义一个名为user"的局部变量.
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.
您可以使用 :as => "another_name" 指定不同的变量名称.
You can specify a different variable name with :as => "another_name".
这里有记录:http://api.rubyonrails.org/classes/ActionView/PartialRenderer.html,这里:http://apidock.com/rails/ActionView/PartialRenderer
...对于较旧的 Rails(版本 <= v3.09):http://apidock.com/rails/ActionView/Partials
...and for older Rails (version <= v3.09): http://apidock.com/rails/ActionView/Partials
这篇关于渲染部分 :object 与 :locals的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!