I’m having a bit of a problem. I want posts to be the default resource when navigating to my site (it’s a blog:)
www.example.com/ #=> posts#index
www.example.com/15 #=> posts#show
etc. However, I want to be able to open up an API that I can use on my portfolio. I can use the respond_to format.json block to allow this:
www.example.com/15.json
but I can’t figure out how to output the index action as json. Here are my current routes:
resources :posts, :path => ''
How would I access the index action with a JSON extension? Sorry If that didn’t make any sense.
You mean: how to write an url in your browser’s address bar to get index as JSON?
Just keep your root index path to
posts#indexbut allow API calls to/posts/all.json(you must code theallaction yourself, of course) – you can also use additional subdomainapi.example.comfor your API calls and return only JSON formatted output for them, like here Dailymile is doing.Also:
I think it’s better for an API to have a clue word like
/posts/<what I want from posts>instead of a blank index/– old, good saying that good function name is the best function documentation.