I''m really new to rendering formats with rails and would appreciate a little help with a json issue. I have created a booking model in which I would like to sync with a jquery calendar called "full calendar" through json. They have an example json script output with the following: [ { "id":1, "title":"Event1", "start":"2009-05-10", }, { "id":2, "title":"Event2", "start":"2009-05-20", "end":"2009-05-22", "url":"http:\/\/yahoo.com\/" } ] When using the json format in rails I get: [ { "booking":{ "updated_at":"2009-05-01T15:42:44Z", "title":"Booking", "property_id":1, "id":1, "end":"2009-05-19", "start":"2009-05-06", "created_at":"2009-05-01T15:42:44Z" } }, { "booking":{ "updated_at":"2009-05-06T09:09:48Z", "title":"Booking", "property_id":1, "id":2, "end":"2009-08-26", "start":"2009-08-19", "created_at":"2009-05-06T09:09:48Z" } } ] How do I change how the json format is rendered within rails 2. I need to get rid of "booking", "updated_at", property_id" and created_at". Thanks for any help.
I think you can do something like this... @booking.to_json(:only=>["title","id","start","end","url"]) On May 6, 10:13 am, lyntaff <l...-XFkl2lNNjp7BrJGnFWTVgQ@public.gmane.org> wrote:> I''m really new to rendering formats with rails and would appreciate a > little help with a json issue. > > I have created a booking model in which I would like to sync with a > jquery calendar called "full calendar" through json. They have an > example json script output with the following: > > [ > { > "id":1, > "title":"Event1", > "start":"2009-05-10", > }, > { > "id":2, > "title":"Event2", > "start":"2009-05-20", > "end":"2009-05-22", > "url":"http:\/\/yahoo.com\/" > } > ] > > When using the json format in rails I get: > > [ > { > "booking":{ > "updated_at":"2009-05-01T15:42:44Z", > "title":"Booking", > "property_id":1, > "id":1, > "end":"2009-05-19", > "start":"2009-05-06", > "created_at":"2009-05-01T15:42:44Z" > } > }, > { > "booking":{ > "updated_at":"2009-05-06T09:09:48Z", > "title":"Booking", > "property_id":1, > "id":2, > "end":"2009-08-26", > "start":"2009-08-19", > "created_at":"2009-05-06T09:09:48Z" > } > } > ] > > How do I change how the json format is rendered within rails 2. I need > to get rid of "booking", "updated_at", property_id" and created_at". > > Thanks for any help.
You can avoid getting the table name - which I take it is "booking" in your case - by telling ActiveRecord not to include that with the following code ActiveRecord::Base.include_root_in_json = false You can filter the fields you want in your database query and then to_json on the query result should give you what you need without the table name. -S -- Posted via http://www.ruby-forum.com/.
you can use except option in to_json Example : @booking.to_json(:except=>[:updated_at,:property_id,:created_at] and to get rid of booking. From rails 2.1+ you should configure in config/initializers/new_rails_defaults.rb file : ActiveRecord::Base.include_root_in_json = false On May 6, 9:13 pm, lyntaff <l...-XFkl2lNNjp7BrJGnFWTVgQ@public.gmane.org> wrote:> I''m really new to rendering formats with rails and would appreciate a > little help with a json issue. > > I have created a booking model in which I would like to sync with a > jquery calendar called "full calendar" through json. They have an > example json script output with the following: > > [ > { > "id":1, > "title":"Event1", > "start":"2009-05-10", > }, > { > "id":2, > "title":"Event2", > "start":"2009-05-20", > "end":"2009-05-22", > "url":"http:\/\/yahoo.com\/" > } > ] > > When using the json format in rails I get: > > [ > { > "booking":{ > "updated_at":"2009-05-01T15:42:44Z", > "title":"Booking", > "property_id":1, > "id":1, > "end":"2009-05-19", > "start":"2009-05-06", > "created_at":"2009-05-01T15:42:44Z" > } > }, > { > "booking":{ > "updated_at":"2009-05-06T09:09:48Z", > "title":"Booking", > "property_id":1, > "id":2, > "end":"2009-08-26", > "start":"2009-08-19", > "created_at":"2009-05-06T09:09:48Z" > } > } > ] > > How do I change how the json format is rendered within rails 2. I need > to get rid of "booking", "updated_at", property_id" and created_at". > > Thanks for any help.