sscirrus
sscirrus

Reputation: 56709

Render Rails query as JSON for jQuery

I have a javascript function that takes data from Rails and presents it.

The format it needs:

[
  {
    title: 'All Day Event',
    start: new Date(y, m, 1)
  },
  {
    title: 'Long Event',
    start: new Date(y, m, d-5),
    end: new Date(y, m, d-2)
  }
]

I'm trying to present this data directly from Rails:

<%= raw @bookings.as_json( only: [:title, :start, :end] ).collect{|o| o["booking"]} %>

This is pretty close, producing:

[{"end"=>nil, "start"=>nil, "title"=>...}, {...}, {...}]

All I have left to do now is change each => into :.

  1. Am I going about this problem the best way?
  2. If so, how can I make this final substitution in hash format?

Upvotes: 1

Views: 82

Answers (1)

Peeyush
Peeyush

Reputation: 6464

Try using to_json instead of as_json. as_json is old syntax and hence may not work.

Upvotes: 1

Related Questions