2

I am currently working with a REST API that has an object with an attribute "name."

item = person.find(id)

I can loop through the item's attributes without any problem and one attribute is, indeed, "name." However, when I try the following:

item.attributes["name"] = "New Name"

I can confirm that the attribute "name" has been updated, yet after calling

item.save

results in an ActiveResource::BadRequest (400) error. Has anybody encountered a problem like this before?

Thanks!

I tried the above and I also tried

item = person.find(id)
item.name = "New Name"
item.save

Finally, per suggestion, I tried

item.update_attribute(:name, "New Name")

and omitted item.save since the save is performed inherently.

It seems that no matter what I try, I continue to see the following stacktrace:

ActiveResource::BadRequest (Failed.  Response code = 400.  Response message = Bad Request.): 
app/controllers/home_controller.rb:84:in `update_data'

Where Line 84 is the event where the ActiveResource object is being updated.

1 Answers1

0

Looks like attributes= is deprecated on the latest stable version of Rails. The last existing version (v3.1.0).

Instead, you can use update_attribute to update the record's attribute:

item.update_attribute(:name, "New Name")

and update_attribute will save the record (if valid), so you don't have to call save on it.

K M Rakibul Islam
  • 33,760
  • 12
  • 89
  • 110
  • Thank you. I also tried update_attribute, to no avail (same error). – prion_niopr Aug 26 '15 at 15:57
  • can you show your full code snippet? also which version of Rails are you using? – K M Rakibul Islam Aug 26 '15 at 16:01
  • Thank you for being patient with me. The stack trace is posted above and I am trying to get a more verbose one as we speak. – prion_niopr Aug 26 '15 at 16:49
  • Can you post the full controller's code? Along with your strong parameters. Also, the full stack trace means everything from your rails log – K M Rakibul Islam Aug 26 '15 at 17:43
  • 1
    Thank you so much, K M Rakibul Islam. I was unable to find the logs easily because one of our team members had added a gem that overrode the traditional Rails logging. I found out the issue and it was completely unrelated - I failed to convert an attribute to an acceptable type, so my json was being formatted improperly. This is my first post. I will try to be more helpful in the future in providing my question along with necessary information. Thanks again. – prion_niopr Aug 28 '15 at 13:48