ruby-on-rails - 渲染部分:collection => @array specify variable name

标签 ruby-on-rails

我正在渲染这样的部分:

$("#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/

相关文章:

jquery - 为什么 application.js 中的这个 Rails JQuery 代码不起作用?

ruby-on-rails - 从参数中删除空格

ruby-on-rails - 在Rails中使用HTTP GET请求进行CSRF保护

ruby-on-rails - paypal gem 多账户 ruby​​ on rails

ruby-on-rails - RestClient::Request.execute传递哈希集

javascript - 一个id可以有多个highcharts吗?

ruby-on-rails - 错误 : ERROR: argument of WHERE must be type boolean, 未键入整数

ruby-on-rails - Ruby 3 动态关键字参数

ruby-on-rails - 在 CSS 中访问 Rails Controller 实例变量

ruby-on-rails - 无差异访问的哈希