0

私は Rabl を試していますが、実質的に空の json ブロックを受け取っているようです。

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

出力

[{"rentable_property":{}}]

注: 単に @r.to_json を使用すると、正しくレンダリングされます。

[{"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}]

rabl が機能しない理由は何か分かりますか?

4

1 に答える 1

2

RABL のドキュメント ( https://github.com/nesquena/rabl#overview ) には、JSON で表示する属性を正確に指定する必要があると書かれています。

彼らの例:

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

にアクセスすると、次の JSON または XML が出力されます/posts.json

[{  "post" :
  {
    "id" : 5, title: "...", subject: "...",
    "user" : { full_name : "..." },
    "read" : true
  }
}]
于 2013-08-22T19:51:54.513 に答える