2017-08-30 66 views
-1

实例变量未传递给partial,因为我面临未定义的方法错误。实例变量未传递给部分(rails 5)

文章控制器:

def index 
    @articles = Article.order("created_at DESC").all 
    end 

条部分(_article.html.erb)

<h3><%= link_to article.title, article_path(article) %></h3> 

index.html.erb

<%= render 'article', article: @articles %> 

通过局部变量的替代方式没” t work too:

<%= render 'article', :article => @articles %> 
+0

你肯定@articles是有朝一日能设置? – xyious

+0

根据rails 5文档,您可以简单地使用:'render @ articles'并确保'@ articles'确实被设置。 – Magnuss

+0

你的代码不起作用,因为'ActiveRecord :: Collection'上没有方法'title'。 – Ilya

回答

0

你在你的<%= render 'article', article: @articles %>

传球@articles集合作为article也许下面将工作?

index.html.erb

<% @articles.each do |article| %> 
    <%= render 'article', article: article %> 
<% end %> 
+1

更好:'<%=渲染部分:'文章',集合:@articles%>',不需要循环。 – Leito

+0

哦,我没有这个! :) 很酷的东西。谢谢! –

相关问题