Ruby on Rails : How can i display data for the current view from a different table

Posted by krishkule on Stack Overflow See other posts from Stack Overflow or by krishkule
Published on 2012-04-08T17:16:22Z Indexed on 2012/04/08 17:30 UTC
Read the original article Hit count: 186

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about ruby

Related posts about table