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

Rendering nested JBuilder partials requires formats: [:json] to be passed #134

Closed
petedmarsh opened this issue May 17, 2013 · 11 comments
Closed

Comments

@petedmarsh
Copy link

I render JSON using inside some HAML views in order to ship along an initial blob of JSON to the client. Since the change in #120 I have found that rendering JSON/JBuilder partials is a little bit more cumbersome as formats: [:json] is no longer passed by default.

Where previously I had written:

json.partial! 'xs/x', my_local: @some_value

I now have had to change this to:

json.partial! partial: 'xs/x', formats: [:json], locals: { my_local: @some_value }

If I do not pass formats: [:json] I get a "Template not found" error. Ideally I'd like to go back to something like the original. Admittedly this only happens as I am rending a JBuilder template with nested partials into a HAML view, so perhaps it is too rare a use case to support directly.

My initial thought was a new method which is like partial! but can only render other JBuilder templates. After thinking about it for a little while, it's probably not a good solution.

@rwz
Copy link
Collaborator

rwz commented May 25, 2013

Can you make an example app showing how partial lookup fails? That should not happen and must be a bug, but I can't recreate it

@petedmarsh
Copy link
Author

I'm also struggling to re-create this outside of my project. That does suggest it is something peculiar to my setup. I'll close this issue and re-open if I can reproduce it.

@sgrif
Copy link

sgrif commented Jul 9, 2013

I'm able to recreate this issue any time I'm rendering a jbuilder partial from inside of an html template.

@petedmarsh
Copy link
Author

@sgrif can you isolate this behaviour into a test case or simple rails app? I couldn't manage to recreate it outside of my entire project.

@rwz
Copy link
Collaborator

rwz commented Jul 9, 2013

Yeah, isolated showcase would be very helpful

@ineu
Copy link

ineu commented Jul 21, 2013

Here is simplest rails app to reproduce this issue: https://github.com/ineu/jbuilder-format-example
In rails 4 this code gives no errors or warnings.

In rails 3.2.13 there's error:

Missing template comments/index with {:locale=>[:en], :formats=>[:html], :handlers=>[:erb, :builder, :jbuilder]}. Searched in:
  * "/home/ineu/work/jbuilder-format-example/app/views"

In rails 3.2.14rc warning:

DEPRECATION WARNING: Passing the format in the template name is deprecated. Please pass render with :formats => [:json] instead. (called from _app_views_posts_foo_html_erb___1163884036962988498_70020346108500 at /home/ineu/work/jbuilder-format-example/app/views/posts/foo.html.erb:2)

but it passes correct formats: :formats=>[:json, :html].

This way it is possible to use jbuilder this way in rails 3.2.14rc2 with no errors or warnings if you change
https://github.com/ineu/jbuilder-format-example/blob/master/app/views/posts/foo.html.erb#L2
to

window.CommentData = <%= raw render(template: 'comments/index', formats: [:json]) %>;

@petedmarsh petedmarsh reopened this Jul 24, 2013
@rwz
Copy link
Collaborator

rwz commented Jul 24, 2013

@ineu that is due to a bug in rails that was fixed in 3.2.14: rails/rails#8406

@rwz
Copy link
Collaborator

rwz commented Jul 24, 2013

@petedmarsh does upgrading to 3.2.14 solve your problem?

@ineu
Copy link

ineu commented Jul 24, 2013

@rwz, with 3.2.14 my example works great, thanks!

@ryanb
Copy link

ryanb commented Jul 28, 2020

I'm running into this in Rails 6. Perhaps it has regressed? I find formats: [:json] necessary otherwise I get the missing template error.

@ryosk7
Copy link

ryosk7 commented Apr 19, 2022

I'm running in Rails 7.
I add formats: [:json] and get template error.

It's solved just set like this.

- render 'index', formats: :json, handlers: 'jbuilder'
+ render 'index'

I don't understand why I occurred this error.

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

6 participants