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 @contacts as contact. How do I do that?

推荐答案

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

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 %>

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

Ruby-on-rails相关问答推荐

如何在嵌套路由中使用Ruby on rails多态关联

链轮轨道V4链接到外部Ruby 的 list 文件

是否从已转换为字符串的数组中提取符号?

为什么在使用 Ruby on Rails 时我的徽标没有出现在 Bootstrap 导航栏中?

无法加载此类文件 -- mysql2/mysql2

如何从 Rails 路由中删除控制器名称?

Rails 3 中的动态错误页面

RSpec 设置会话对象

使用 javascript 提交 Rails 远程表单

如何在 Rails 1.2.3 中使复选框默认为选中?

Rails:将所有未知路由重定向到 root_url

在 Rails 中复制模型实例

counter_cache 实现的问题

rbenv:没有 gemsets 生存

submit_tag rails 表单中的 bootstrap 图标

Rails 3.1 中的 Rails.cache 错误 - TypeError: can't dump hash with default proc

如何强制 RSpec 测试失败?

在 Ruby on Rails 中的控制器中获取 URL 的锚部分

Rails 路由:GET 没有参数 :id

没有UTC的Rails 3默认日期时间格式