2012-03-16 3 views
2

Im 누군가가 간단한 실수 일 가능성을 밝힐 수 있기를 희망합니다. 로컬 변수 _article.html.erb에있는 article을 _article.html.erb에 중첩 된 다른 부분으로 전달하려고합니다. 부분 코드가 _article.html.erb에있을 때 잘 동작합니다. 많은 변수 (지역 변수 포함)를 시도했지만 로컬 변수를 전달하지 못하는 것 같습니다.Rails 3에서 로컬 객체를 중첩 된 부분에 전달합니다.

<%= form_for current_user.favorites.find_by_article_id(article), :html => { :method => :delete, :class => 'unfavorite_form', }, :remote => true do |f| %> 
      <div><%= f.hidden_field :article_id %></div> 
      <%= image_submit_tag("vote-favorite-on.png", :alt => "Favorite", :id => "favorites_button", :title => "Remove from favorites") %> 
<% end %> 

오류

_article.html.erb

<% if current_user.favorited?(article) %>  
      <%= render :partial => 'unfavorite', :object => article %> 
<% else %> 
      <%= render :partial => 'favorite', :object => article %> 
<% end %> 

_favorite.html.erb (좋아하고 싫어하는 모두가 더 많거나 적은 동일, 그래서 하나를 게시 한) 메시지는 다음과 같습니다 rendering에 대한

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

답변

2

레일의 문서는 다음과 같이 객체의 사용을 언급 :

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

그리고 그 말 : 변수는 부분의 이름으로 번역되어 객체 : 그것은처럼 보일 수

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

합니다. 다음 명시 할 수 있기 때문에, 개인적으로

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

내가 주민 구문을 선호 : 그래서 귀하의 경우, _favorite에, 당신이 좋아하는 변수를 사용해야 할 것

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

덕분에, 그 생각했다 나는 긍정적이었다. 나는 그것을 게시하기 전에 시험했다. –

+0

예, 그런 경우 발생합니다.) – pschuegr

관련 문제