Rails 4.0.0 で active_model_serializers を使用して、json 応答をシリアル化しようとしています。ただし、使用すると、AMSクラスがまったく呼び出されたり使用されたりしていないようです
render json: user
構成に問題があるのか、名前空間を間違って使用しているのかはわかりません。
Gemfileには次のものがあります:
gem 'active_model_serializers', '0.8.0'
コントローラー (app/controllers/api/v1/users_controller.rb 内):
module API
module V1
class UsersController < API::V1::BaseController
def show
user = User.find(params[:id])
render json: user
end
end
end
end
UserSerializer (app/serializers/user_serializer.rb 内)
class UserSerializer < ActiveModel::Serializer
attributes :id, :first_name, :last_name
end
しかし、json 応答では、まだ完全なオブジェクトが表示されています。
{"id":1,"first_name":"Test","last_name":"User1","created_at":"2013-12-26T07:12:02.618Z","updated_at":"2013-12-26T07:12:02.618Z"}
render json: user, serializer: UserSerializer
明示的に呼び出すことから、シリアライザーを API::V1 にネームスペースすることまで、すべてを試しましたが、結果は同じです。ここで何か不足していますか?
編集:これは、継承ではなくBaseController
継承を行うことで修正されました。ただし、 に何が欠けていたのかは完全にはわかりません。ActionController::Base
ActionController::Metal
ActionController::Metal