ruby on rails 3.1 - How can I render partial in show.html.erb -
i have these 2 models in ruby on rails application - artist , song. associated follows:
class artist < activerecord::base
has_many :songs
attr_accessible :artist_name
and
class song < activerecord::base
belongs_to :artist
attr_accessible :title, :track_url, :artist_id
i have code in views/artist/show.html.erb:
<%= render 'artist_song' %> <table> <% @artist.songs.each |song| %> <tr> <td><%= song.title %></td> </tr> <% end %> </table>
the partial im trying render(_artist_song.html.erb) in same view looks this:
<table> <% @artist = artist.all %> <% @artist.each |artist| %> <tr> <td><%= link_to artist.artist_name, artist %></td> </tr> <% end %> </table>
the way suppose work when click on artist shown trough partial, code below partial has show me songs belongs particular artist. both partial , code in table tag working individually. when put them together, looks there conflict between them , server showing me no method error:
nomethoderror in artists#show showing c:/sites/oml/app/views/artists/show.html.erb line #9 raised: undefined method `songs' #<array:0x5fe1418> extracted source (around line #9): 6: 7: 8: <table> 9: <% @artist.songs.each |song| %> 10: <tr> 11: <td><%= song.title %></td> 12: </tr> rails.root: c:/sites/oml application trace | framework trace | full trace app/views/artists/show.html.erb:9:in `_app_views_artists_show_html_erb__950110288_54062208' app/controllers/artists_controller.rb:21:in `show'
i couldn`t find solution. appreciated. thank you.
you not using partial. need render partial inside artist
loop:
show.html.erb
<table> <% @artists = artist.all %> <% @artists.each |artist| %> <tr> <td><%= link_to artist.artist_name, artist %></td> </tr> <%= render :partial => 'artist_song', :artist => artist %> <% end %> </table>
this way, passing current artist
object inside partial, there can do:
artist_song.html.erb
<% artist.songs.each |song| %> <tr> <td><%= song.title %></td> </tr> <% end %>
Comments
Post a Comment