Giter Site home page Giter Site logo

Comments (12)

ryanb avatar ryanb commented on May 29, 2024 4

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.

rgarver avatar rgarver commented on May 29, 2024

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.

rgarver avatar rgarver commented on May 29, 2024

The latest master fixes this for me.

from jbuilder.

mutewinter avatar mutewinter commented on May 29, 2024

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.

Sephi-Chan avatar Sephi-Chan commented on May 29, 2024

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.

shobee avatar shobee commented on May 29, 2024

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.

Sephi-Chan avatar Sephi-Chan commented on May 29, 2024

@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.

mutewinter avatar mutewinter commented on May 29, 2024

@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.

Sephi-Chan avatar Sephi-Chan commented on May 29, 2024

This jsonobject 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.

mutewinter avatar mutewinter commented on May 29, 2024

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.

bwillis avatar bwillis commented on May 29, 2024

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.

vedmant avatar vedmant commented on May 29, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.