Rails - empty json file when using jbuilder

I am trying to use jbuilder to create a json file from an event list. It must be in a specific format. Currently, no matter what I put in the jbuilder file, when you go to /1/events.json users, the file is empty (just {}).

I tried to put the response to the block in the controller, but then it just returns the list of events in the wrong format.

I think the routes were executed correctly because the log file says that the file myevents.json.jbuilder was successfully displayed when url / users / 1 / myevents.json was requested. Also, when .html is requested, the myevents.html file displays well, with all the information in it.

I am really a dead end in this, any help would be greatly appreciated!

I have listed all the relevant code if anyone can pick something up:

event controller:

def myevents @events = current_user.events end 

myevents.json.jbuilder:

 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 

environment.rb:

 Jbuilder.key_format :camelize => :lower 

routes.rb:

 match 'users/:id/events' => 'events#myevents' 
+4
source share
3 answers

The reason is that you need to put the following line in your RSPEC controller helper file:

 config.render_views = true 

I had the same problem: https://github.com/rails/jbuilder/issues/32

+4
source

As far as I know, you can just skip

Jbuilder.encode

Try without this block, perhaps everything will be fine.

+3
source

Figured it out. For some reason, he doesn't like the Jbuilder.encode line do | json | although this is what the documentation says to use. The response syntax is still not quite right.

+1
source

Source: https://habr.com/ru/post/1439372/


All Articles