<% @songs.each do |song| %> <div class="col-md-4 col-sm-6 portfolio-item"> <div class="portfolio-caption"> <h4 class="aaa"><%= link_to song.title, song %><br></h4> </div> </div> <% end %>
このなかでは使えている、<%= link_to song.title, song %>のtitleメソッドが
<%= link_to @songs.title, song %> ←これを追加するとエラーが出る <% @songs.each do |song| %> <div class="col-md-4 col-sm-6 portfolio-item"> <div class="portfolio-caption"> <h4 class="aaa"><%= link_to song.title, song %><br></h4> </div> </div> <% end %>
このようにすると「undefined method `title' for #Song::ActiveRecord_Relation:0x007feef4b1c688」と、「titleメソッドが定義されていません」
というエラーが出るのですが、なぜでしょうか?
class SongsController < ApplicationController before_action :set_song, only: [:show, :edit, :update, :destroy] before_action :authenticate_user!, except: [:index, :show] # GET /songs # GET /songs.json def index if params[:user_id] @user = User.find(params[:user_id]) @songs = @user.songs.order(:cached_votes_up => :desc)# ここでアソシエーションが生きる else @songs = Song.all.order(:cached_votes_up => :desc) end end
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/20 04:30 編集