This is a Rails plugin (Railtie) that allows your existing RESTful controllers to work over JSONP.
REST is designed around the four HTTP request methods (
JSONP requests are always
GETs. To get around this restriction, this Railtie lets you specify the
desired method in a special
For example, to make a
PUT request to
/users/1.json, you would make a JSONP (
GET) request to
The plugin also adds a custom Responder for JSONP that automatically adds special behaviour to
respond_with (via a custom
For JSONP requests handled with
:callback => param[:callback]will be automatically added to the
:unprocessable_entity) will be changed to
:accepted, since error responses cannot be handled by JSONP. To detect errors in JSONP responses check for an
errorattribute in the returned data.
First, install the gem:
gem install restful_jsonp
Then add this to your Rails app's
Note that this only works for Rails 3. In principle you could try to swap in the RestfulJSONP::MethodOverride middleware into a Rails 2.3+ app, but this has not been tested.
To enable the custom JSONP Responder, add this to your
ApplicationController (or any
controller that you want to enable the responder for):
self.responder = RestfulJSONP::JSONPResponder
How it Works
_method functionality is built in to Rails (via Rack), but is normally only available for
POST requests. This Railtie replaces the default
Rack::MethodOverride middleware with a slightly
altered version that checks for the
_method parameter regardless of whether it's in a
Note that this functionality is enabled for all requests, regardless of whether they are done via JSONP or otherwise.
- Fixed error response processing in JSONPResponder
- Initial release