Search code examples
ruby-on-railsangularjsrabl

What is wrong with my RABL file?


Rabl is not generating the main object but it is generating it for its children nodes.

I have a Rails-Angularjs project. I want to use RABL to generate the json file. I have read the RABL's documentation and created the Rabl file for the action show, which is not working properly, as explained:

object @ticket => :ticket do
  attributes :id, :name, :subname, :description, :width, :height, :qty, :single, :double, :project_id, :material_id, :equipment_id, :location_id, :created_at, :updated_at
end

child :project do
  attributes :name, :nickname, :project_number
end 

child :job_state do 
  attributes :color, :name 
end

child :location do
  attributes :name
end

child :local_equipment do
  attributes :name
end

child :local_material do
  attributes :name
end

IF I remove 'do' and 'end' on the object @ticket, It throws the error: 'wrong number of arguments (1 for 0)'.

If I leave them, it doesn't show any ticket's field name. As shown below:

{"ticket":{"project":{"name":"Information Security Conference 2017","nickname":"infosecon17","project_number":1000},"job_state":{"color":"red","name":"printed"},"location":{"name":"Grand prairie graphics"},"local_equipment":null,"local_material":null}}

Any help will be appreciated.


Solution

  • If the issue is not with your Angular, from rails side, this is the structure that works for me. Check that your Tickets#show file is here and named: app\views\tickets\show.json.rabl and contains only

    object @ticket
    attributes :id, :name, :subname, :description, :width, :height, :qty, :single, :double, :project_id, :material_id, :equipment_id, :location_id, :created_at, :updated_at
    

    for the children you would have separate files like this: app\views\tickets\projects.json.rabl which will contain

    collection @projects
    extends "projects/show"
    

    but also confirm that attributes match your schema anyway.