RESTful Content Consolidation in Rails

I want to implement content negotiation on some resources in a Rails application. I use Mootools and probably can configure the content type accepted by XMLHTTPRequest to "application / json".

Is there a way to get this information in my controller and generate JSON responses instead of XHTML?

I try to avoid doing something like:

http://site/resource/1?format=JSON 

... since it loads my url, imposes a certain degree of redundancy and is not so flexible.

Thanks!

+6
json rest mootools ruby-on-rails content-negotiation
source share
4 answers

You can use the respond_to side in your controller method, for example:

 respond_to do |format| format.html { # Generate an HTML response... } format.json { # Generate a JSON response... } end 

Rails determines the response format based on the value of the Accept HTTP header sent by the client.

+4
source share

http: //site/resource/1.json misuse of content negotiation. The fact is that the URL should remain the same, but the client requests a specific presentation (JSON, PDF, HTML, etc.) based on the HTTP headers that it sends with the request.

+8
source share

Is http: //site/resource/1.json supposed to work? you may need to configure it in a Rails environment, however, depending on how current the version of Rails you have, I doubt it.

+1
source share

After much research, while the rails have everything to automatically select a template for output, it still requires a reply_to call for each one you want to support.

0
source share

All Articles