NoMethodError for 'update' in Ruby

136 views Asked by At

I've been having trouble for a while getting this put method to work.

I'm getting the following error message on the 'success' line:

NoMethodError - undefined method `update!' for nil:NilClass

Please see the code below:

#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

There are 1 answers

1
RustyToms On BEST ANSWER

You need to fix how you are retrieving your model and how you are updating it, so change your code to this:

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