我有这两个模型在我的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