0

私はいくつかの継承構造を使用するプロジェクトに取り組んでいます...基本クラスは..

# /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"
    }
  ]
},
4

1 に答える 1

1

提供したデータに従って、テンプレートをに配置し/app/views/circlesます。/app/views/api/v1/circlesエラーは、代わりにそれらを入れる必要があることを示していると私は信じています。

2番目の質問では、各円に関連する形状が多数あると言っているようです。その場合、私は次のようなものがあなたが望むものを与えるはずだと信じていますviews/api/v1/circles/show.json.rabl

# views/api/v1/circles/show.json.rabl
object @circle
extends 'api/v1/shapes/show'
attributes :radius
child(:shapes) do
  extends 'api/v1/shapes/show'
end
于 2013-02-10T00:45:43.103 に答える