Ruby-on-rails 渲染部分 :collection => @array 指定变量名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12188381/
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-02 20:42:30  来源:igfitidea点击:

Render partial :collection => @array specify variable name

ruby-on-rails

提问by botbot

I am rendering a partial like this:

我正在渲染这样的部分:

$("#box_container").html("<%= escape_javascript( render :partial => 'contacts/contact_tile', :collection => @contacts) %>")

Problem is that my partial is expecting the variable 'contact'.

问题是我的部分期望变量“联系”。

ActionView::Template::Error (undefined local variable or method `contact'

I simply want to tell the partial to expect a variable contact. Should iterate through @contactsas contact. How do I do that?

我只是想告诉部分期待一个变量contact。应该遍历@contactsas contact。我怎么做?

回答by botbot

Found this is also helpful from the docs. You aren't limited to having the variable named after the partial:

从文档中发现这也很有帮助。您不仅限于以部分命名的变量:

http://guides.rubyonrails.org/layouts_and_rendering.html

http://guides.rubyonrails.org/layouts_and_rendering.html

To use a custom local variable name within the partial, specify the :as option in the call to the partial:

要在部分中使用自定义局部变量名称,请在对部分的调用中指定 :as 选项:

<%= render :partial => "product", :collection => @products, :as => :item %>

With this change, you can access an instance of the @products collection as the item local variable within the partial."

通过此更改,您可以访问 @products 集合的实例作为部分中的项目局部变量。”

回答by matthew.tuck

The documentation at http://guides.rubyonrails.org/layouts_and_rendering.htmlsays:

http://guides.rubyonrails.org/layouts_and_rendering.html 上的文档说:

When a partial is called with a pluralized collection, then the individual instances of the partial have access to the member of the collection being rendered via a variable named after the partial.

当使用复数集合调用局部时,局部的各个实例可以通过以局部命名的变量访问正在呈现的集合成员。

So it will be passed a variable called "contact_tile" instead of "contact". Perhaps you can just rename your partial.

所以它将传递一个名为“contact_tile”而不是“contact”的变量。也许你可以重命名你的部分。

If this naming is important, you could do it explicitly without the collection option by something like:

如果此命名很重要,您可以通过以下方式在不使用集合选项的情况下显式执行此操作:

@contacts.each { |contact| render :partial => 'contacts/contact_tile', :locals => {:contact => contact } }

(although as a commenter pointed out, this may not be as performant)

(尽管正如评论者指出的那样,这可能没有那么高效)

回答by Manish Shrivastava

Latest syntax are :

最新的语法是:

index.html.erb

index.html.erb

<%= render partial: "product", collection: @products %>

_product.html.erb

_product.html.erb

<p>Product Name: <%= product.name %></p>

@productsis used in partial as product

@products部分用作 product

Where @products can be considered as Product.alland productcan be considered as a row of product i.e. Product.firstas looped all product one by one.

其中@products 可以被视为Product.all并且product可以被视为一行产品,即Product.first一个一个循环的所有产品。