Ruby on Rails : How can i display data for the current view from a different table
- by krishkule
This might be a easy-to-answer question, but its a pain in the bum for me....
So...
I have two tables -
Chords - |id|chord|name|rating|artist_id|
Artists - |id|artist|
(the relations :
chords : belongs_to :artist
artists : has_many :chords
)
And in the index page for "chords"
i want to display chord,name,rating from Chords table
AND artist from the Artists table
this is the code for the chrod's index.html.erb :
<table border="1">
<%@chords.each do |chord|%>
<tr>
<td><%=chord.artist.artist%></td>
<td><%= link_to chord.name, chord %></td>
<td><%=chord.rating%></td>
<td><%= chord.created_at %></td>
</tr>
<%end%>
</table>
The error message is : undefined method `artist' for nil:NilClass
Actually, at first it worked, but when i started to create the "new.html.erb" page and the create,new controllers, it stopped working - thats why this is so confusing for me!
I will be glad to hear any critique and suggestions!
Thank you