Create custom child node with RABL

5.8k Views Asked by At

I'd like to create JSON like this:

{
  "field1": "value-1",
  "field2": "value-2",
  "actions": {
    "edit": "/edit/action/url"
  }
}

using rabl. It doesn't work with child method nor i can't do it with node - got unknown method error. So, is it possible to create custom child node using rabl template? Say something like this:

collection @datas => :datas
attributes :field1, :field2
child :actions do
  node :edit do
    edit_datas_path :id
  end
end

edit
I choose this one:

node :actions do
  actions = {}
  actions[:edit] = edit_customer_group_path(:id)
  node :foo do
    foos = {}
    foos[:bar] = "hello"
    foos
  end
  actions
end

But accepted answer below is correct too.

1

There are 1 best solutions below

0
On BEST ANSWER

I just stumbled across this issue the other day. This ticket in RABL issues helped me. In your case, I was able to generate proper JSON using:

collection @datas
extends "datas/base"
node :actions do
  {:edit => root_path}
end

generated JSON:

{
  id: 1,
  actions: {
            edit: "/"
           }
}

Using RABL 0.5.3