我有這兩個模型在我的Ruby on Rails應用程序 - 藝術家和歌曲。它們有關聯如下:我如何渲染部分在show.html.erb
class Artist < ActiveRecord::Base
has_many :songs
attr_accessible :artist_name
和
class Song < ActiveRecord::Base
belongs_to :artist
attr_accessible :title, :track_URL, :artist_id
我有這樣的代碼中的意見/藝術家/ show.html.erb:
<%= render 'artist_song' %>
<table>
<% @artist.songs.each do |song| %>
<tr>
<td><%= song.title %></td>
</tr>
<% end %>
</table>
的部分我試着去渲染( _artist_song.html.erb)在相同視圖中看起來像這樣:
<table>
<% @artist = Artist.all %>
<% @artist.each do |artist| %>
<tr>
<td><%= link_to artist.artist_name, artist %></td>
</tr>
<% end %>
</table>
它假定工作的方式是當我點擊顯示在部分圖片上的藝術家時,部分下面的代碼必須向我展示屬於特定藝術家的所有歌曲。 表格標籤中的部分代碼和代碼都單獨運行。但是,當我把它們放在一起,看起來他們之間有衝突,服務器出了我這個沒有方法錯誤:
NoMethodError in Artists#show
Showing C:/Sites/OML/app/views/artists/show.html.erb where line #9 raised:
undefined method `songs' for #<Array:0x5fe1418>
Extracted source (around line #9):
6:
7:
8: <table>
9: <% @artist.songs.each do |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'
我couldn`t找到一個解決方案。任何幫助將不勝感激。 謝謝。
廣告d @artist =控制器中的Artist.all用於顯示方法 – Shrikant1712 2013-04-10 09:12:31
您如何設置'@ artist'?它是一個ActiveRecord模型嗎? – Mab879 2013-04-12 03:31:11
藝術家是一個ActiveRecord模型是的 – ibechev 2013-04-16 13:20:34