Giter VIP home page Giter VIP logo

Comments (7)

rwz avatar rwz commented on June 2, 2024

What json do you want to get?

from jbuilder.

burtondav avatar burtondav commented on June 2, 2024

I'm trying to get json for an ancestry gem table. The table contains a tree of location data. Because it's a tree, I only need to start with the first record - then I access their children. This is the code I'm trying:

json.locations do
  json.label @location.name
  json.children @location.children do |child|
    json.label child.name
    json.children child.children do |child2|
        json.label child2.name
        json.children child2.children do |child3|
               json.label child3.name
          end
   end
  end
end

The results start with a { and jqTree requires that it start with a [

This is what I'm getting:

jbuilder output

BTW - If I use the following, the output starts with [

But, this produces the wrong data.

json.array!(@locations) do |location|
  json.label location.name
  json.children location.children do |child|
    json.label child.name
    json.children child.children do |child2|
        json.label child2.name
            json.children child2.children do |child3|
               json.label child3.name
       end
   end
  end

end

from jbuilder.

jdeff avatar jdeff commented on June 2, 2024

First, I might want to use a partial for this instead of having a child2, child3, etc. Unless you only always have three levels. Maybe something like

_your_partial.json.jbuilder:

json.label location.name
json.children do
    json.array! location.children do |child|
        json.partial! 'your_partial', :location => child
    end
end

In order to get an array at the top level, in your controller, put @location inside of an array (i.e. @locations = [@location]). Then your view would look like:

json.locations do
    json.array! @locations do |location|
        json.partial! 'your_partial', :location => location
    end
end

from jbuilder.

burtondav avatar burtondav commented on June 2, 2024

My controller was like this (I only need the first location to start the tree):

    def tree
     @location = Location.find(1)
    end

I tried this - but, got errors.

def tree
  @location = [Location.find(1)]
end

from jbuilder.

burtondav avatar burtondav commented on June 2, 2024

I tried this:

json.locations do
 json.label @location.name
 json.children @location.children do |child|
 ...

But, it still starts with { instead of [

from jbuilder.

rwz avatar rwz commented on June 2, 2024

You should use array! to get an array.

from jbuilder.

jdeff avatar jdeff commented on June 2, 2024

You could do:

json.array! [1] do
json.location do
...
end
end

Sent from my iPhone

On Feb 27, 2013, at 12:17 PM, David Burton [email protected] wrote:

I tried this:

json.locations do
json.label @location.name
json.children @location.children do |child|
...
But, it still starts with { instead of [


Reply to this email directly or view it on GitHub.

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.