私はいくつかの継承構造を使用するプロジェクトに取り組んでいます...基本クラスは..
# /app/models/shape.rb
class Shape < ActiveRecord::Base
acts_as_superclass # https://github.com/mhuggins/multiple_table_inheritance
end
サブクラスは...
# /app/models/circle.rb
class Circle < ActiveRecord::Base
inherits_from :shape
end
これは、継承構造を示す図です。
これらのモデルのために、 RABL gemを使用して API を作成しようとしています。関連するコントローラーは次のとおりです...
# /app/controllers/api/v1/base_controller.rb
class Api::V1::BaseController < InheritedResources::Base
load_and_authorize_resource
respond_to :json, except: [:new, :edit]
end
...
# /app/controllers/api/v1/shapes_controller.rb
class Api::V1::ShapesController < Api::V1::BaseController
actions :index
end
end
...
# /app/controllers/api/v1/circles_controller.rb
class Api::V1::CirclesController < Api::V1::BaseController
def index
@circles = Circle.all
end
def show
@circle = Circle.find(params[:id])
end
end
Ryan Bates の Railscast #322 でshow
提案されているテンプレートを作成しました。このように見えます...
# /app/views/circles/show.json.rabl
object @circle
attributes :id
http://localhost:3000/api/v1/circles/1.json
以下のエラーメッセージでサークルをリクエストすると表示されます...
テンプレートがありません
{:locale=>[:en]、:formats=>[:html]、:handlers= のテンプレート api/v1/circles/show、api/v1/base/show、inherited_resources/base/show、application/show がありません>[:erb, :builder, :arb, :haml, :rabl]}.
継承されたリソースを操作するには、どのようにテンプレートを設定する必要がありますか?
部分的な成功
以下の見解を思いつきました。また、モデルの継承構造を実装して、コードを DRY に保つこともできました。
# views/api/v1/shapes/index.json.rabl
collection @shapes
extends "api/v1/shapes/show"
...
# views/api/v1/shapes/show.json.rabl
object @place
attributes :id, :area, :circumference
...
# views/api/v1/circles/index.json.rabl
collection @circles
extends "api/v1/circles/show"
...
# views/api/v1/circles/show.json.rabl
object @circle
extends "api/v1/shapes/show"
attributes :radius
if action_name == "show"
attributes :shapes
end
これにより、円に必要な JSON が出力されます (index
アクション)。
# http://localhost:3000/api/v1/cirles.json
[
{
"id" : 1,
"area" : 20,
"circumference" : 13,
"radius" : 6
},
{
"id" : 2,
"area" : 10,
"circumference" : 4,
"radius: 3
}
]
しかし、何らかの理由で関連付けられたすべての属性が出力されません...
注:モデルには、前に言及しなかった関連付けがあります。shapes
Shape
# http://localhost:3000/api/v1/cirles/1.json
{
"id" : 1,
"area" : 20,
"circumference" : 13,
"radius" : 6,
"shapes" : [
{
"id" : 2,
"created_at" : "2013-02-09T12:50:33Z",
"updated_at" : "2013-02-09T12:50:33Z"
}
]
},