1

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?

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

I get [ {"booking":{...}}, {"booking":{...}}, {"booking":{...}} ]. I need to get rid of the extra "bookings" so it's just [{...},{...},{...}].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.