0

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
Phil Hudson
  • 3,819
  • 8
  • 35
  • 59
  • What are you getting with your `puts @download_edit`? I think you have a couple problems, but one of them is `@download_edit = Download.get(:download_id => edit_id)` – RustyToms Dec 09 '13 at 19:34
  • Just a blank line now which is strange :S – Phil Hudson Dec 09 '13 at 19:36
  • Yeah it's coming out as nil :/ – Phil Hudson Dec 09 '13 at 19:36
  • 1
    Change that line to `@download_edit = Download.get(edit_id)` – RustyToms Dec 09 '13 at 19:36
  • # ArgumentError - wrong number of arguments (0 for 1): – Phil Hudson Dec 09 '13 at 19:38
  • okay, so next we need to change the success line. Try replacing that line with `success = @download_edit.update_attributes(data)` – RustyToms Dec 09 '13 at 19:43
  • RuntimeError - Download#update_attributes is deprecated, use Download#update... It runs without error when using just update (as updated in the code above) however it fails to actually amend the data in the database, which is strange :S – Phil Hudson Dec 09 '13 at 19:45
  • Don't correct your question. Once it is sorted out I will post an answer which you can accept, future users need to see what went wrong and how it was fixed. Please leave the question with the incorrect code. – RustyToms Dec 09 '13 at 19:45
  • Sure I will revert it, cheers. – Phil Hudson Dec 09 '13 at 19:46
  • OK, use `update` instead of `update_attributes`. I think the problem is that you have not made these attributes `attr_accessible` in your model. Please update your question with the code from your download.rb file – RustyToms Dec 09 '13 at 19:46
  • Ok, I'm going to type up an answer with the last change, then it should work. If it does, feel free to accept my answer! Also, you are using rails 4, correct? If so, add a ruby-on-rails-4 tag to this question. – RustyToms Dec 09 '13 at 19:50
  • Awesome, will do if it does! Many thanks. Nope, using Sinatra. :) – Phil Hudson Dec 09 '13 at 19:51
  • Wait a minute, you aren't using Rails? How did I miss that. If my answer works I'm going to be surprised. Try it out and see what happens, I don't know why I thought you were using rails. – RustyToms Dec 09 '13 at 19:59
  • If my answer doesn't work, I think you still have the same problem, you just need the Sinatra equivalent to `attr_accessible` – RustyToms Dec 09 '13 at 20:00
  • Yeah sorry about that! And it didn't work unfortunately :( I'm using Sinatra with Datamapper :) – Phil Hudson Dec 09 '13 at 20:00
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/42808/discussion-between-rustytoms-and-phil-hudson) – RustyToms Dec 09 '13 at 20:03

1 Answers1

1

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
RustyToms
  • 7,600
  • 1
  • 27
  • 36
  • Sorry, not sure if this is strictly a Rails or ActiveRecord thing, but I'm not getting this error: /App/lib/Download.rb:3:in `': undefined method `attr_accessible' for Download:Class (NoMethodError) – Phil Hudson Dec 09 '13 at 19:58