In Rails 2.3 I always used
render :json => { :success => true, :data => @foobar}
to send JSON data to my frontend. In Rails 3 I'm using
respond_to :json
...
respond_with @foobar
But what I'm missing: I need the 'success' value within the JSON structure. What's the right way to inject such data into JSON response in Rails 3?
Hm, tried this too, but I get the following error as result:
SyntaxError (app/controllers/properties_controller.rb:13: syntax error, unexpected tASSOC, expecting '}'
respond_with { :success => true, :data => @property }
^
/app/controllers/properties_controller.rb:13: Can't assign to true
respond_with { :success => true, :data => @property }
^
app/controllers/properties_controller.rb:13: syntax error, unexpected tASSOC, expecting tCOLON2 or '[' or '.'
respond_with { :success => true, :data => @property }
When things doesn't fit the default, you need to go back to the previous customized way.
respond_with
accepts a block.
respond_with @foobar do |format|
format.json { render :json => { :success => true, :data => @foobar} }
end
You can't use the object like value. You just add some key/value inside with override serializable_hash
method
But you can generate your hash in respond_with
respond_with { :success => true, :data => @foobar}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With