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
Post a Comment