Reputation: 10580
I have this basic association setup on both User
and Recipe
models as in User has many recipes
and on view I'm referencing one of recipe attribute like this
<%= @recipe.title %>
And the error is
undefined method `title' for #<ActiveRecord::Associations::CollectionProxy::ActiveRecord_Associations_CollectionProxy_Recipe:0x00000101edeac0>
I did some research and it seems like this has to do with something called proxy object that I will need to access associated object's attributes in a different way but I find it challenging to digest this concept.
At a higher level, I need to achieve showing selected recipe in detail (show) from the list (index) and showing this detail part is where the error is.
Controller
def index
@recipes = Recipe.all
end
def show
end
View
<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>
Upvotes: 0
Views: 61
Reputation: 4386
@recipes = Recipe.all returns associations you should do something like this
<%- @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 %>
It will iterate through collection and display each recipe
Upvotes: 1