我正在渲染这样的部分:
$("#box_container").html("<%= escape_javascript( render :partial => 'contacts/contact_tile', :collection => @contacts) %>")
问题是我的部分期望变量“contact”。
ActionView::Template::Error (undefined local variable or method `contact'
我只是想告诉部分需要一个变量contact
。应将 @contacts
作为 contact
进行迭代。我该怎么做?
最佳答案
从文档中发现这也很有帮助。您不限于以部分名称命名的变量:
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:
<%= render :partial => "product", :collection => @products, :as => :item %>
通过此更改,您可以访问 @products 集合的实例作为部分中的项目局部变量。”
关于ruby-on-rails - 渲染部分:collection => @array specify variable name,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12188381/