Add JSON support to Rails app
Posted
by Meltemi
on Stack Overflow
See other posts from Stack Overflow
or by Meltemi
Published on 2010-05-28T01:25:51Z
Indexed on
2010/05/28
1:31 UTC
Read the original article
Hit count: 368
I am experimenting with Rails and was wondering what's needed to allow/add support for JSON requests?
I have a vanilla installation of Rails 2.3.5 and the default scaffolding seem to provide support for HTML
& XML
requests but not JSON
.
class EventsController < ApplicationController
# GET /events
# GET /events.xml
def index
@events = Event.all
respond_to do |format|
format.html # index.html.erb
format.xml { render :xml => @events }
end
end
# GET /events/1
# GET /events/1.xml
def show
@event = Event.find(params[:id])
respond_to do |format|
format.html # show.html.erb
format.xml { render :xml => @event }
end
end
...
I'm new to this but it would appear as though i would need to add a format line in each method along the lines of:
format.js { render :js => @event.json }
couldn't this be done automatically? perhaps there's a template somewhere i need to update...or a flag i can set? Or perhaps, and most likely, I've missed the boat entirely?!?
© Stack Overflow or respective owner