事前に助けてくれてありがとう。
モデル MasterSong の属性としてデータベースに id3 タグ データを設定したいと考えています。具体的には、タイトル、アーティスト、アルバムです。
create_table :master_songs do |t|
t.integer :user_id
t.has_attached_file :m_song
**- t.string :title
- t.string :artist
- t.string :album**
私のモデルでは、オブジェクト (master_song) が保存される前にコールバックを使用してデータを設定しています。
before_save :set_id3_tags_in_database
def set_id3_tags_in_database
TagLib::MPEG::File.open(self.m_song.path) do |file|
tag = file.id3v2_tag
tag.title
tag.album
tag.artist
end
self.update_attributes(:title => tag.title,
:artist => tag.artist,
:album => tag.album)
end
私の構文に問題があると確信しています。オブジェクトにエラーが発生したようです
undefined method `title' for nil:NilClass with an infinite loop
コントローラの作成アクションは正常です:
def create
@master_song = current_user.master_songs.build(params[:master_song])
respond_to do |format|
if @master_song.save
format.html { redirect_to @master_song, notice: 'Master song was successfullycreated.' }
end
ここで何が間違っていますか?更新: def set_id3_tags z = TagLib::MPEG::File.open(self.m_song.path) do |file| tag = file.id3v2_tag tag.title end self.update_attribute!(:title => z) end
スタック レベルが深すぎるというエラーが表示されるようになりました。