gazay / gon

Your Rails variables in your JS
MIT License
3.05k stars 184 forks source link

Jbuilder partial not working #123

Open mnort9 opened 10 years ago

mnort9 commented 10 years ago

Jbuilder partial doesn't seem to be working. I'm receiving this error:

undefined local variable or methoditem' 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
danhper commented 10 years ago

I am having the same problem here. It seems like as and collection are treated as normal variables.

jarinudom commented 9 years ago

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)
denisovlev commented 9 years ago

Is there any solution?

danhper commented 9 years ago

I ended up using active_model_serializer instead of jbuilder, it worked much better for me.

gazay commented 9 years ago

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 =)

denisovlev commented 9 years ago

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

gazay commented 9 years ago

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.

gazay commented 9 years ago

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)

denisovlev commented 9 years ago

Won't this gem https://github.com/yappbox/render_anywhere help you?

gazay commented 9 years ago

It will call double render error if you use it in controller