Pre-editing
<%@songs.each do|song|%>
<div class="col-md-4col-sm-6portfolio-item">
<div class="portfolio-caption">
<h4class="aaa">%=link_to song.title, song%>br>/h4>
</div>
</div>
Among them, the title method of <%=link_to song.title, song%>
is
Edit
<%=link_to @songs.title, song%>← Adding this causes an error
<%@songs.each do | song | %>
<div class="col-md-4col-sm-6portfolio-item">
<div class="portfolio-caption">
<h4class="aaa">%=link_to song.title, song%>br>/h4>
</div>
</div>
<%end%>
If I do this, I will get the following two errors, but why?
undefined method `title' for <Song::ActiveRecord_Relation:------>
title method is not defined
↓song.controller
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])
@[email protected](:cached_votes_up=>:desc)
else
@songs=Song.all.order(:cached_votes_up=>:desc)
end
end
@Summary from the comments of Mr. Pork Spout
@ is full-width.
※ If it's full-width, it should be a different error, so I think it's a typo.
@songs
is not the Song model, but Song::ActiveRecord_Relation.Since it is a collection, there is no method called title
, which must be taken out as a model before using it.
For example, ['abc', 'cdf'].upcase
.An aggregate must be treated as an aggregate, and it does not allow the method to fly to its contents.
© 2024 OneMinuteCode. All rights reserved.