艺术家/ show.html.erb
<% if @artist.songs.any? %>
<div class="col-md-8">
<% @artist.songs.each do |song| -%>
<span><%= link_to song.title, song.artist %></span>
<% end -%>
</div>
<% end %>
的routes.rb
resources :artists, :path => "/Music", only: [:show] do
resources :songs, :path => "/", only: [:show]
end
艺术家控制器
class ArtistsController < ApplicationController
def show
@artist = Artist.friendly.find(params[:id])
end
end
歌曲控制器
class SongsController < ApplicationController
def show
artist = Artist.friendly.find(params[:artist_id])
@song = artist.songs.find(params[:id])
end
end
艺术家模特
class Artist < ActiveRecord::Base
extend FriendlyId
friendly_id :name, use: :slugged
has_many :songs, dependent: :destroy
validates :name, presence: true, length: { maximum: 50 },
uniqueness: { case_sensitive: false }
def slug=(value)
if value.present?
write_attribute(:slug, value)
end
end
end
歌曲模型
class Song < ActiveRecord::Base
belongs_to :artist
default_scope -> { order(:title) }
validates :artist_id, presence: true
validates :title, presence: true,
length: { maximum: 140 }
rails_admin do
configure :artist do
label 'artist name: '
end
end
的Gemfile
source 'https://rubygems.org'
gem 'rails', '4.2.1'
gem 'friendly_id', '~> 5.1.0'
gem 'sorcery'
gem 'validates_email_format_of'
gem 'bootstrap-sass', '3.2.0.0'
gem 'sqlite3', '1.3.10'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.1.0'
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 2.0'
gem 'sdoc', '0.4.0', group: :doc
gem 'delayed_job_active_record'
gem 'rails_admin'
gem 'font-awesome-sass-rails'
gem 'coffee-script-source', '1.8.0'
group :development, :test do
gem 'byebug'
gem 'web-console', '~> 2.0'
gem 'minitest'
end
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
当我进入 http://localhost:3000/Music/ed-sheeran
它给我这个错误信息
undefined method `to_model' for 1:Fixnum
我可能遗漏了一些明显的东西。