2013-12-09 37 views
0

我一直在麻煩一會兒讓這個put方法工作。Ruby中'更新'的NoMethodError

我收到以下錯誤消息的「成功」行!

NoMethodError - 未定義的方法'更新」對於零:NilClass

請參閱下面的代碼:

#edit download 
put '/view1/downloadedit' do 
    data = JSON.parse(request.body.read) 
    edit_id = data[0]["downloadID"] 
    @download_edit = Download.get(:download_id => edit_id) 
    puts @download_edit 
    success = @download_edit.update![0][data] 
    if success 
    status 201 
    puts 'edit saved okay' 
    else 
    status 201 
    puts 'edit failed to SAVE' 
    end 

end 

Download.rb

#class download 
class Download 
    include DataMapper::Resource 
    property :downloadID, Serial, key: true 
    property :PageID, String 
    property :title, String 
    property :dlLink, String 
    property :imgSrc, String 
    property :caption, String 
    property :dlLive, Integer 
    property :createdAt, DateTime 
    property :user_id, Integer 
end 
+0

你用'puts @ download_edit'得到了什麼?我認爲你有幾個問題,但其中一個是'@ download_edit = Download.get(:download_id => edit_id)' – RustyToms

+0

現在只是一個空行,這是奇怪的:S –

+0

是的它是作爲零出現:/ –

回答

1

您需要修改你如何獲取你的模型和你如何更新,所以變化你的代碼如下:

put '/view1/downloadedit' do 
    data = JSON.parse(request.body.read) 
    edit_id = data[0]["downloadID"] 
    @download_edit = Download.get(edit_id) 
    puts @download_edit 
    success = @download_edit.update(
    attribute1: data[0][attribute1] 
    attribute2: data[0][attribute2] 
    # and so on for all the other attributes... 
) 
    if success 
    status 201 
    puts 'edit saved okay' 
    else 
    status 201 
    puts 'edit failed to SAVE' 
    end 

end 
+0

對不起,如果這是嚴格的Rails或ActiveRecord的事情,但我沒有得到這個錯誤:/App/lib/Download.rb:3:in'':undefined方法'attr_accessible'下載:Class(NoMethodError) –

+1

是的,堅持下去,讓我試着修復它... – RustyToms