Rails 3: How To Return A JSON Document? -


i'm in process of creating simple api application. have create controllers , did processing. however, view being returned.

how can return json document user instead of view?

also, if there's error, should return json document or... ?

thanks!

add controller

   respond_to :html, :json 

(remove html if want json-api).

in actions can use respond_with call, like:

   respond_with(@posts = post.all) 

it render json-object if format json, , render html-view if html requested. if routes created standard resources, adding .json suffix url give json-result, ie. if route specified as:

   resources :posts 

Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -