render :json => 'string here' expected result

Viewed 20669

I have done this a thousand times but I'm still not comfortable with how the render :json handles strings.

To set a scope let's talk about Rails 3

This is how it behaves right now:

...
render :json => 'This is the string'
...

Will be returning to the browser:

This is the string

That's actually not a valid JSON response :S

Ideally it should be rendering something like this:

"This is the string"

Even the rails guides say:

You don’t need to call to_json on the object that you want to render. If you use the :json option, render will automatically call to_json for you.

And calling "This is the string".to_json is actually returning "\"This is the string\"" as expected.

"This is the string".to_json #=> "\"This is the string\""

Am I so wrong ?

1 Answers
Related