Comments (12)
This has resurfaced for me in Rails 6. Passing the formats: [:json]
option is necessary otherwise I get the missing template error.
json.posts list.posts.order('created_at DESC'), partial: 'jbuilders/post.json.builder', as: :post
Including the extension resolves the missing template error but has a couple unwanted side effects:
- Relative locales do not lookup the template path correctly (it includes the extension).
- The fragment cache key includes the
json.jbuilder
extension. If you render the partial a different way elsewhere it will generate two caches since they have a different key.
Specifying the formats
option resolves these two issues for me.
from jbuilder.
I was just noticing that too. I was worried that rails/rails#5308 had resurfaced, but (without rigorous testing yet) it seems to be jbuilder specific. <----WRONG
Scratch that. Redid my testing and I'm still seeing the problem on master also.
from jbuilder.
The latest master fixes this for me.
from jbuilder.
I'm also getting this error, even when using the latest master. Switching the partial to .json fixes the problem, but causes deprecation warning.
from jbuilder.
Any news? Is there something I can do to help fixing it?
JBuilder is a great way to seed Javascript objects: it's very useful for client side apps.
If you depreciate it today, it means that you will break it. If this bug remains, our use case will be broken. :(
from jbuilder.
Today we faced the same problem but previously everything was fine. Since we have updated rails to 3.2.6 and some other gems this issue occurs.
from jbuilder.
@shobee I ended up writing this very simple helper in my ApplicationHelper
:
def json_partial!(json, template, locals)
json.partial!(partial: template, formats: [:json], handlers: [:jbuilder], locals: locals)
end
So now, instead of writing:
json.partial!('vehicles/vehicle_part_purchases.json.jbuilder', vehicle: vehicle)
I write:
json_partial!(json, 'vehicles/vehicle_urls', vehicle: vehicle)
from jbuilder.
@Sephi-Chan I'm trying your helper idea, but I'm getting an error with the json
object not being declared in my view context. Did you do anything special to get jbuilder's json object into the view scope?
from jbuilder.
This json
object is available in any my_view.json.jbuilder
. How do you use it?
For instance, in versions/index.json.jbuilder
:
json.array! @versions do |json, version|
json_partial!(json, 'versions/version', version: version)
end
And when I use it to seed JSON in my view, I have brands/seeds/brand.js.coffee:
json_partial!(json, '/brands/brand', brand: brand)
Then in the Haml view data_sheets/brands.html.haml
:
= seed_data('type', 'vehicle_types/seeds/type', type: @type)
With the following code for seed_data
defined in ApplicationHelper
:
def seed_data(property, template_name, locals, options = {})
options.reverse_merge!(prefix: 'window.Seeds')
file = Rails.root.join('app', 'views', template_name)
json = render(file: file, formats: [:json], handlers: [:jbuilder], locals: locals)
key = "#{options[:prefix]}.#{property}"
code = "#{key} = #{json};\n"
javascript_tag { raw(code) }
end
from jbuilder.
Ah, I was trying to use the helper in my haml view, not in the jbuilder files themselves. I am able to get the json_partial! helper to be called, but it's returning nothing when called inside my jbuilder files.
Here's an example
projects/show.json.jbuilder
json_partial!(json, "projects/show", project: @project)
It is my intent for this to include the contents of the projects/_show.json.jbuilder partial. I have this in a partial since I need to return the same thing for the create and update actions in my controller.
The partial does work if I change my code to
json.test json_partial!(json, "projects/show", project: @project)
This isn't ideal, however, since I don't want a root tag for this json output.
from jbuilder.
This has been fixed in this commit: e2a6616 and is available in gem version 0.4.3.
This allows you to supply a hash to override settings like formats, handlers, etc., but make sure you explicitly set the partial name with :partial and any locals:
json.test json_partial! partial: "projects/show", formats: :json, locals: { project: @project }
from jbuilder.
I have the same issue:
This doesn't work:
json.posts list.posts.order('created_at DESC'), partial: 'jbuilders/post', as: :post
And this works:
json.posts list.posts.order('created_at DESC'), partial: 'jbuilders/post.json.builder', as: :post
from jbuilder.
Related Issues (20)
- `json.array! @reports, :id, :count` always give me all the attributes.[Not an issue] HOT 1
- JBuilder 2.11.3: "NoMethodError Exception: undefined method `empty?` for PG::Result" HOT 7
- Cache keys - recyclable by default?
- JBuilder 2.11.3: uninitialized constant ActionView::AbstractRenderer on load under Nginx/Passsenger HOT 3
- Cache key depending on multiple dependencies HOT 1
- jBuilder 2.11.3, Rails 6.1.4.1 fail to render objects that are child of Simpledelegator HOT 2
- 2.11.3 and 2.11.4 missing from Changelog HOT 1
- Does not work with Rails 7 api_only = true HOT 7
- Incorrect url comment notes in generated scaffold controller in a namespace.
- ActionView::MissingTemplate after upgrading to Rails 7 HOT 3
- Auto-rendering ActionText body contents instead of their attributes hash
- JBuilder Template Test failing on CI HOT 5
- I am moving my rails application to rails only api but jbuilder is returning a blank screen HOT 5
- Can't seem to render unescaped HTML in Jbuilder HOT 2
- Passing locals to `json.array!` ? HOT 2
- `json.partial!` line in template is not executed when passing locals using new hash syntax HOT 2
- Jbuilder ignore_nil, key_format, and deep_format_keys not working (Rails 7) HOT 1
- Jbuilder ignore_nil, key_format, and deep_format_keys not working (Rails 7)
- Sample use in standalone Ruby script HOT 1
- ActionView 7.0.7 collection caching breaking on jbuilder arrays HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jbuilder.