感謝您提前提供任何幫助。RoR TagLib-Ruby:如何在DB中設置ID3標籤
我想在我的數據庫中設置id3標籤數據作爲我的模型MasterSong的屬性。具體來說,標題,藝術家和專輯。
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 | 標籤= file.id3v2_tag tag.title 結束 self.update_attribute!(:標題=> Z) 結束
GET棧層次過深的錯誤了。
您還應該檢查'tag'是否爲零。爲什麼不直接通過'self.title = tag.title'和'self來設置塊的屬性。album = tag.album'等。 – robinst
OK。我改變了我的答案。你怎麼看? – Jches