user2889046 user2889046 - 3 months ago 9
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 https://www.reddit.com/r/pics/.json 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?

Answer

First of all define your routes as resources

resources :xvaziris

Just append the url with .json

Here is an example:

http://localhost:3000/xvaziris.json

json example

Comments