0

I think I found a bug with the to_json method in Rails.

I have generated a fresh scaffold and modified the create method to render the errors in JSON format.

The to_json method is removing the array from the returned hash.

Here is the result of a debug session.

(rdb:2) @recipe.errors {:name=>["can't be blank"]}

(rdb:2) @recipe.errors.to_json "{\"name\":\"can't be blank\"}"

As you can see, the array has been removed.

Has anyone else found this?

Cheers,

Tim

2
  • What does {:name=>["can't be blank"]}.to_json do in the Rails console? Commented Jun 20, 2011 at 7:09
  • If I try and convert @recipe.errors.to_json then it doesn't work but if I copy the exact hash that @recipe.errors produces and run to_json on that, then it works. Bizarre. Commented Jun 20, 2011 at 23:13

1 Answer 1

1
irb(main):003:0> {:name=>["can't be blank"]}.to_json
=> "{"name":["can't be blank"]}"

I use ruby1.9.2, and I don't find the bug above. May be the problem has been resolved:)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.