user2889046 user2889046 - 1 year ago 72
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?

Answer Source

First of all define your routes as resources

resources :xvaziris

Just append the url with .json

Here is an example:


json example

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download