2

I'm trying to building some json output from rails using jbuilder. i have something like:

  json.links do | i |
    i.array!( @links ) do | j, link |
      j.source link['source']
      j.target link['target']
      j.stats do | s |
        [ 'item1', 'item2' ].each { |item|
          s.item statistics_path( :source => link['source'], :metric => item )
        }
      end
    end
  end

of course, this results in something like:

  {
    links: [
      source: "a",
      target: "b",
      stats: {
        item: 'url for item2'
      }
    ]
  }

when i actually want something like:

  {
    links: [
      source: "a",
      target: "b",
      stats: {
        item1: 'url for item1',
        item2: 'url for item2'
      }
    ]
  }

any ideas?

1 Answer 1

7

You can use:

json.set!(:item1, "stuff")

In your case:

  json.links do | i |
    i.array!( @links ) do | j, link |
      j.source link['source']
      j.target link['target']
      j.stats do | s |
        [ 'item1', 'item2' ].each { |item|
          s.set!(item, statistics_path( :source => link['source'], :metric => item ))
        }
      end
    end
  end
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.