Karan
Karan

Reputation: 15094

Rabl showing an empty json block

I am trying Rabl, however, I seem to receive a practically empty json block.

require_dependency "api/application_controller"

module Api
  class RentablePropertiesController < ApplicationController
    respond_to :json
    def index
      @r = Core::RentableProperty.all
      # render :text => @r.to_json  --> note: this renders the json correctly
      render "api/rentable_properties/index"  #note: rabl here does not
    end
  end
end

index.json.rabl collection @r

Output

[{"rentable_property":{}}]

Note: with a simply @r.to_json, it renders correctly:

[{"id":1,"description":"description","property_type_id":1,"created_at":"2013-08-22T19:04:35.000Z","updated_at":"2013-08-22T19:04:35.000Z","title":"Some Title","rooms":null,"amount":2000.0,"tenure":null}]

Any idea why rabl doesn't work?

Upvotes: 0

Views: 832

Answers (1)

MrYoshiji
MrYoshiji

Reputation: 54882

The documentation of RABL (https://github.com/nesquena/rabl#overview) says that you need to precise what attributes you want to show in your JSON.

Their example:

# app/views/posts/index.rabl
collection @posts
attributes :id, :title, :subject
child(:user) { attributes :full_name }
node(:read) { |post| post.read_by?(@user) }

Which would output the following JSON or XML when visiting /posts.json:

[{  "post" :
  {
    "id" : 5, title: "...", subject: "...",
    "user" : { full_name : "..." },
    "read" : true
  }
}]

Upvotes: 2

Related Questions