Ruby中“更新”的NoMethodError

时间:2013-12-09 19:31:40

标签: ruby sinatra ruby-datamapper

我一直在努力让这个put方法起作用。

我在'成功'一行收到以下错误消息:

NoMethodError - 未定义的方法`update!'为零: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

1 个答案:

答案 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