user2889046 user2889046 - 2 months ago 4
JSON Question

Rails - Rendering a page in JSON based conditionally on the URL?

I have a page I want to conditionally render in json based on if there is either .json or /.json at the end of that URL, like how reddit does it, for example renders the page in json. Rendering the page in json is easy enough with the

render json: @some_data
, however I'm not really sure how to make this conditionally based on the url. How do I go about setting this up?


First of all define your routes as resources

resources :xvaziris

Just append the url with .json

Here is an example:


json example