Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Gon's Jbuilder partial! pathing differs from standard #54

Closed
dznz opened this issue Jul 9, 2012 · 10 comments
Closed

Gon's Jbuilder partial! pathing differs from standard #54

dznz opened this issue Jul 9, 2012 · 10 comments

Comments

@dznz
Copy link

dznz commented Jul 9, 2012

It looks like Gon's Jbuilder module attempts to do its own partial evalutation before passing the result to Jbuilder. This means that only root-level paths are allowed for partials used via a gon.jbuilder call, which is non-standard.

For example, gon.jbuilder requires something like:

  • partial! "app/views/widgets/_widget.json.jbuilder"

Jbuilder itself accepts one of the following:

  • partial! "widgets/widget"
  • partial! "widget"

Because these two methods differ, they are incompatible. Gon jbuilder partials cannot be used independently of Gon.

The best solution would be to remove any custom Gon partial evalutation and find a way to call JbuilderTemplate.encode with enough context for Jbuilder to resolve its own partials.

A workaround that we are using looks like this:

class PagesController < ApplicationController
    def home
        @widgets = Widget.all
        gon.widgets = JSON.parse(render_to_string(template: "widgets/index.json.jbuilder"))
        render content_type: 'text/html', formats: [:html], layout: 'application'
    end
end

As you can see, it's pretty horrible and I'm sure there is a smoother workaround, but ideally we could just use the gon.jbuilder method directly.

I hope this is enough detail to explain the problem; let me know if anything is unclear!

@gazay
Copy link
Owner

gazay commented Jul 9, 2012

Thank you for response!

All clear here, I'll fix it right when have some time to do opensource) Hope will do it soon.

@dznz
Copy link
Author

dznz commented Jul 9, 2012

Thanks! Sorry I couldn't submit a patch for it, wasn't quite sure what else Gon was doing, and how to ensure the context was setup correctly.

@gazay
Copy link
Owner

gazay commented Jul 24, 2012

Fixed. Soon will be release of 4.0.0

@gazay gazay closed this as completed Jul 24, 2012
@boom
Copy link

boom commented Jul 26, 2012

This is still not fully fixed. Partials only works with jbuilder syntax that specify directory and passed variable:

json.comments @comments do |json, comment|
  json.partial! 'comments/comment', :comment => comment
end

But will not work with alternative short hand syntax for jbuilder that uses default folder structure and file naming (assuming same files and folders as specified above):

json.comments @comments do |json, comment|
  json.partial! comment
end

@gazay gazay reopened this Jul 28, 2012
@dznz
Copy link
Author

dznz commented Aug 7, 2012

@gazay thanks heaps for the partial (hurr) fix, in any case!

@dznz
Copy link
Author

dznz commented Aug 7, 2012

I'm looking at 7c05c6e and I don't want to be nit-picky, but should Gon be parsing the partials at all? Seems to me like Gon should set up the environment and then delegate directly to jbuilder and let it do all the partial resolving.

Otherwise you are basically going to be stuck re-implementing jbuilder's code, with bugs turning up every time Gon's approach differs from jbuilder's approach. I reckon @mroseboom's issue might be solved in this approach.

I recognise that "set up the environment" is pretty hand-wavey, and ultimately there might be a good reason not to do it this way, but I'd be interested to know your thoughts.

@dznz
Copy link
Author

dznz commented Aug 7, 2012

An example: I'd like to do this in a controller:

@widgets = current_user.widgets
gon.jbuilder template: "app/views/widgets/index.json.jbuilder", as: "widgets"

Where widgets/index.json.jbuilder looks like:

json.array!(@widgets) do |json, widget|
  json.partial! "widgets/widget", widget: widget
end

and widgets/_widget.json.jbuilder looks like this:

json.(widget, :id, :name, :label)

But when I try it, I get: undefined local variable or method 'workout' for Gon::Jbuilder:Module.

I'm pretty sure this is a parsing bug, but I'd rather see Gon lean on jbuilder for parsing than have it patched in Gon, if you take my meaning.

Not sure if this helps!

@gazay
Copy link
Owner

gazay commented Aug 11, 2012

I'm sorry, but I'll be on vacation soon. So if you'll do some research and do pull request - it will be super great!))

I want to comeback to maintain this gem after vacation - 2-3 weeks, but I glad that you use this gem and try to find some capabilities to use it :) If you'll share to me your experience of usage this gem I'll can do my work better

Thank you)

@elsurudo
Copy link

elsurudo commented Aug 7, 2013

Having the same issue over here, so this was never fixed? @gazay is there any particular reason that gon does its own parsing of the partials? It seems to me that that should be left to the jbuilder (or rabl, etc.) gem itself...

@gazay
Copy link
Owner

gazay commented Sep 23, 2013

Because of architecture of jbuilder. It is replaces flow of usual rendering, so it allow you to render only once through controller action. That's why I have to partially replace jbuilder functionality (in Rabl I can simply call Rabl::Engine#render method). But it was a year ago - maybe now there is a different situation and if you can help with research - it will be super awesome.

Anyway thank you that you somehow interested in gon and find time to write me some response!

@gazay gazay closed this as completed Nov 13, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants