gon
gon copied to clipboard
Jbuilder partial not working
Jbuilder partial doesn't seem to be working. I'm receiving this error:
undefined local variable or method
item' for Gon::Jbuilder:Module`
show.json.jbuilder
json.partial! 'items/item', collection: @items, as: :item
_item.json.jbuilder
json.id item.id
json.type item.class.to_s
I am having the same problem here.
It seems like as
and collection
are treated as normal variables.
Similar problem here: undefined method 'company' for #<HomeController:0x007f85f392ed40>
# home_controller.rb
gon.jbuilder template: "app/views/api/v1/companies/index.json.jbuilder",
locals: { companies: current_user.companies.decorate },
as: :my_companies
# index.json.jbuilder
json.array!(companies) do |company|
json.partial! "api/v1/companies/company", company: company
end
# _company.json.jbuilder
json.extract! company, :id, :ticker, :name
json.url controller.view_context.company_url(company)
Is there any solution?
I ended up using active_model_serializer instead of jbuilder, it worked much better for me.
Problem is not solved yet as because of specific of jbuilder's rendering process I had to write own parser for its templates. Right now I don't have time and need to fix partial functionality in jbuilder module in gon but I can help if someone want to help with it =)
What's the problem with jbuilder rendering that urged you to write your own parser? As far as I can see jbuilder can handle local variables and templates on its own
I don't know the current situation, but when I added it, there was no explicit parser/renderer/engine included. So you can't do as in rabl, for example, re = Rabl::Engine.new...; re.render
. Jbuilder is highly tied with Rails rendering stack, and you can't use it standalone which I needed for render templates and put them into JSON.
In Rails 5 will be rendering instrument that allow to throw away all Gon::Jbuilder::Parser logic and just render jbuilder template (https://medium.com/evil-martians/new-feature-in-rails-5-render-views-outside-of-actions-2fc1181e86a8)
Won't this gem https://github.com/yappbox/render_anywhere help you?
It will call double render error if you use it in controller