我在User和Recipe模型上都有这个基本的关联设置,就像在User has many recipes中一样,在视图中,我引用了一个配方属性,如下所示
<%= @recipe.title %>错误是
undefined method `title' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Recipe:0x00000101edeac0>我做了一些研究,似乎这与称为代理对象的东西有关,我需要以不同的方式访问相关对象的属性,但我发现消化这个概念很有挑战性。
在更高的层次上,我需要实现从列表(索引)中显示所选食谱的详细信息(show),并显示这个详细信息部分,这就是错误所在。
控制器
def index
@recipes = Recipe.all
end
def show
end视图
<p>
<strong>Title:</strong>
<%= @recipe.title %>
</p>
<p>
<strong>Content:</strong>
<%= @recipe.content %>
</p>
<p>
<strong>Duration:</strong>
<%= @recipe.duration %>
</p>
<p>
<strong>Rating:</strong>
<%= @recipe.rating %>
</p>发布于 2014-11-15 22:38:58
@recipes = Recipe.all返回关联您应该这样做
<%- @recipes.each do |recipe| %>
<p>
<strong>Title:</strong>
<%= recipe.title %>
</p>
<p>
<strong>Content:</strong>
<%= recipe.content %>
</p>
<p>
<strong>Duration:</strong>
<%= recipe.duration %>
</p>
<p>
<strong>Rating:</strong>
<%= recipe.rating %>
</p>
<% end %>它将遍历集合并显示每个配方
https://stackoverflow.com/questions/26946622
复制相似问题