Reputation: 1614
Say if I did a simple scaffold like rails g scaffold user name:string . How would I go through the controller and make the data as jsonp rather than json
Upvotes: 1
Views: 236
Reputation: 28574
EDIT: See Ashley Williams Answer!
JSONP is just wrapping the JSON object in a function call. So something like this
UsersController:
def index
@callback_func_name = params[:callback]
@users = User.all
end
View named index.js.erb
:
<%= @callback_func_name %>(<%= @users.to_json %>)
Then the request you issue is:
http://localhost.com:3000/users.js?callback=myfunction
The details might not be exactly right, but this should get you most of the way there, let me know if you have any questions.
Upvotes: 0
Reputation: 6840
Rails 3 actually allows you to pass a callback
option to render
now:
def index
render json: @users.to_json, callback: params[:callback]
end
Which would be available at:
http://0.0.0.0:3000/users.json?callback=foobar
Upvotes: 2