I'm trying to create a JSONP API for my Rails 3 application. Right now in my controllers, I have a lot of actions which follow this pattern:
# This is from my users_controller.rb, as an example
def index
@users = User.all
respond_with(@users, :callback => params[:callback])
end
While this works as is, I would like to DRY it up by not having to repeat the :callback => params[:callback]
in every action's call to respond_with
. How can I do this?
Update: One thing I've realized that is ugly about my above code is that the :callback => params[:callback]
option will be passed for any response format, not just JSON. The following code is probably more correct:
def index
@users = User.all
respond_with(@users) do |format|
format.json { render :json => @users, :callback => params[:callback]}
end
end
There are a couple ways I've considered to address this problem, but I can't figure out how to make them work:
- Override
render
(perhaps in the application controller) so that it accepts a:jsonp
option that automatically includes the:callback => params[:callback]
parameter. This way I could change the above code to the following, which is somewhat shorter:
def index
@users = User.all
respond_with(@users) do |format|
format.json { render :jsonp => @users}
end
end
- Create a responder that overrides
to_json
in order to solve my problem. That way I could leave out the block and just callrespond_with(@users, :responder => 'MyResponder')
to solve the issue. Or perhaps I could include this code in an application responder using plataformatec's responders gem so thatrespond_with(@users)
by itself would be sufficient.