无法将本地对象传递给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} %>
- Rails中的共享JS(咖啡)
- Heroku上的twitter-bootstrap-rails:Glyphicons显示为正方形
- 如何在不使用eval的情况下将字符串转换为ruby / rails中的哈希?
- 在Ruby on Rails中配置或调整Spree或其他解决方案以创建多供应商,单域市场(类似于etsy)
- 迁移以创建默认值
- rails form_for从不调用create controller动作来使用redirect_to
- 是puma唯一的multithreading导轨4 http服务器?
- 在Mac OS X 10.6(SL)上安装Rails
- Ruby没有安装json -v 1.8.1作为bundle的一部分,ERROR:无法构建gem原生扩展