问题描述
我想传递一个包含原点到特定页面的局部变量,这个变量只包含一个带有值的符号.
I want to pass a local variable that contains the origin to come on a specific page, this variable contains just a symbol with the value.
当我使用此代码时,它运行良好,可以在部分中访问原始变量:
When I use this code it works perfect, the origin variable is accessible in the partial :
render :partial => "products", :collection => @products, :locals => {:origin => :gallery}
但是当我使用此代码时,原点未设置且无法在部分中访问:
But when I use this code, the origin is not set and not accessible in the partial :
render @products, :locals => {:origin => :gallery}
这里有什么不同?第二行代码不像第一行那样渲染局部?
What is the difference here? Is the second line of code not render the partial like the first line?
推荐答案
<%= render @products %>
确实是渲染部分的简写语法.但是使用速记语法,Rails 将忽略 ":locals" 变量.Rails 指南中有更多相关内容.
Is indeed the shorthand syntax for rendering a partial. But with the shorthand syntax, Rails will ignore the ":locals" variable. There's more on this in the Rails Guides.
所以如果你想给渲染传递额外的选项,你必须指定:partial => ...".如果您想知道为什么会发生这种情况,您可以查看 Rails 来源.
So if you want to pass extra options to the render, you have to specify ":partial => ...". If you want to know why this happens, you can take a look at the Rails source.
这篇关于渲染@object 和 locals vs 渲染 :partial的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!