简体   繁体   中英

Rails - using jBuilder to create json file

I am trying to use jBuilder to create a json file (as per the timeline.verite.co timeline requirements) - but the result just comes out as blank, other than a pair of {}. The html version of the file is picking up the correct info, so it isn't because the current user doesn't have any events. Anyone have any idea why? Thanks!

Here is the file ( @events refers to all the events for the current user):

json.key_format! :camelize => :lower
Jbuilder.encode do |json|
    json.timeline do
        json.headline current_user.first_name
        json.type "default"
        json.text "A Timeline"
        json.start_date
        json.array!(@events) do |event|
            json.start_date event.start_date
            json.end_date event.end_date
            json.headline event.headline
            json.text event.text
            json.asset do
                json.media event.media
                json.credit event.credit
                json.caption event.caption
            end
        end
    end
end

UPDATE:

I've tried something really simple, and the file is still coming out blank. I have no idea why!!

This still gives a blank file:

Jbuilder.encode do |json|
    json.id current_user.id
end

I don't need encode to json in view file. Your view file should look like:

json.id current_user.id
json.name current_user.name

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM