Trong Rails 2.3 Tôi luôn luôn sử dụngRails JSON tiêm
render :json => { :success => true, :data => @foobar}
để gửi dữ liệu JSON để frontend của tôi. Trong Rails 3 Tôi đang sử dụng
respond_to :json
...
respond_with @foobar
Nhưng điều tôi thiếu: Tôi cần giá trị 'thành công' trong cấu trúc JSON. Cách đúng để đưa dữ liệu đó vào phản hồi JSON trong Rails 3 là gì?
Hm, cố gắng này quá, nhưng tôi nhận được lỗi sau khi kết quả:
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 }
Hmhm, đã thử điều này, nhưng tôi nhận được lỗi sau là kết quả: – ctp