Rails - Update_attributes现在正在处理未定义的方法错误

时间:2012-07-17 03:59:31

标签: ruby-on-rails-3 update-attributes

这是有效的,但现在我在调用update_attributes时遇到以下错误:

NoMethodError in SongsController#update

undefined method `name' for nil:NilClass
Rails.root: C:/Sites/music3

Application Trace | Framework Trace | Full Trace
app/controllers/songs_controller.rb:164:in `update'

令我困惑的是,我的@song对象中没有“name”。 (见下面的图式),但艺术家做了。歌曲和艺术家通过多对多的关系联系在一起。似乎update_attributes也试图更新艺术家对象?不认为应该这样做。真的不确定这里的任何帮助将不胜感激。

谢谢,

Song Controller
def update
    @artist = Artist.find(params[:artist_id])
    authorize! :update, @artist
    @song = Song.find(params[:form_song_id])
    @song.artists <<  @artist
    @s3_test = params[:s3_name]

    @song.update_attributes(params[:song]) #causing the error  
 end

架构

create_table "artists", :force => true do |t|
    t.string   "name"
    t.string   "city"
    t.string   "province"
    t.string   "country"
    t.text     "influence"
    t.text     "bio"
    t.text     "contact_info"
    t.date     "date_founded"
    t.date     "created_date"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "url_slug"
    t.string   "pay_pal"
    t.string   "image"
  end

  create_table "songs", :force => true do |t|
    t.string   "song_name"
    t.string   "song_artist"
    t.string   "song_contribute_artist"
    t.string   "song_written"
    t.string   "song_licence_type"
    t.string   "song_url_slug"
    t.date     "song_licence_date"
    t.integer  "song_plays"
    t.text     "lyrics"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.string   "s3_name"
    t.string   "download_link"
    t.string   "torrent_link"
    t.string   "s3_id"
    t.integer  "s_a_id"
  end

示例参数。

{"utf8"=>"✓",
 "_method"=>"put",

 "song"=>{"s3_name"=>#<ActionDispatch::Http::UploadedFile:0x260e908 @original_filename="done good (final final).mp3",
 @content_type="audio/mp3",
 @headers="Content-Disposition: form-data; name=\"song[s3_name]\"; filename=\"done good (final final).mp3\"\r\nContent-Type: audio/mp3\r\n",
 @tempfile=#<File:C:/Users/Ted/AppData/Local/Temp/RackMultipart20120716-1600-ohtooj>>,
 "song_name"=>"Done Good",
 "song_artist"=>"Grimes",
 "song_contribute_artist"=>"",
 "song_written"=>"Ted Kennedy",
 "song_licence_type"=>"copywrite",
 "song_licence_date(1i)"=>"2012",
 "song_licence_date(2i)"=>"7",
 "song_licence_date(3i)"=>"17",
 "song_plays"=>"",
 "song_url_slug"=>"",
 "lyrics"=>"None"},
 "commit"=>"Upload",
 "artist_id"=>"1",
 "form_song_id"=>"5",
 "id"=>"create"}

1 个答案:

答案 0 :(得分:0)

试试这个,因为那些不在歌曲课程中。

params[:song].delete("@content_type")
params[:song].delete("@tempfile")
params[:song].delete("@headers")