无法将本地对象传递给Rails 3中的嵌套部分

我希望有人可以阐明可能是一个简单的错误。 我试图将局部变量文章(在部分_article.html.erb中)传递给_article.html.erb中的另一个部分嵌套。 当部分代码在_article.html.erb中时,它工作正常。我尝试了很多变种(包括:本地人),但似乎无法传递局部变量。

_article.html.erb

  'unfavorite', :object => article %>   'favorite', :object => article %>  

_favorite.html.erb(最喜欢的和不喜欢的都或多或少相同,所以我只发了一个)

   { :method => :delete, :class => 'unfavorite_form', }, :remote => true do |f| %> 
"Favorite", :id => "favorites_button", :title => "Remove from favorites") %>

错误消息是:

  undefined local variable or method `article' for #<#:0x6727a58> 

用于渲染的rails文档提到了像这样的对象的使用:

 <%= render :partial => "customer", :object => @new_customer %> 

并说:

 Within the customer partial, the customer variable will refer to @new_customer from the parent view. 

这使得它似乎将:object变量转换为partial的名称。 所以在你的情况下,在_favorite中,你必须使用最喜欢的变量:

  <%= form_for current_user.favorites.find_by_article_id(favorite), :html => { :method => :delete, :class => 'unfavorite_form', }, :remote => true do |f| %> 

我个人更喜欢locals语法,因为那样你可以显式:

 <%= render :partial => 'favorite', :locals => {:article => article} %>