1

I have a model that I only want to ever return JSON, regardless of any conneg or file-like extensions on the URI (e.g. /app/model.json). Google-fu is coming up short and this can't be that difficult.

JStroop
  • 475
  • 1
  • 8
  • 21

2 Answers2

5

In your controllers you simple have to create a respond_to block that only responds to JSON:

respond_to do |format|
  format.json { render :json => @model }
end
Christoph Eicke
  • 1,134
  • 7
  • 15
0

This is actually a decision made by the controller, not because there is/is not a model or view present. In your controller you can:

render json: @your_model

However you will quickly find that the default implementation of to_json (which is what is used internally, above) can be annoying hard to do exactly what you want. When you reach that point you can use RABL to create views that massage the JSON from your model(s) exactly as you want.

cfeduke
  • 23,100
  • 10
  • 61
  • 65