我有这些文件,我可以让ActiveModel Serializer工作。
#
# congif/routes.rb
namespace :dealer do
resources :users do
resources :orders do
get 'search_dealer_profile', on: :collection
#
# app/serializers/profile_serializer.rb
class ProfileSerializer < ActiveModel::Serializer
attributes :id
end
#
# app/controllers/dealer/orders_controller.rb
class Dealer::OrdersController < Dealer::BaseController
def search_dealer_profile
profile = Profile.where(id: params[:id]).first
# I tried
#
# => Return a Json without use AMS. Object {id: 4, user_id: 4, name: "Alex", surname: "Sensation"…}
render json: profile
# => Works in console but here I get this : uninitialized constant Dealer::OrdersController::ProfileSerializer
render json: ProfileSerializer.new(profile).as_json
end
我想要的json
是
{:profile=>{:id=>4}}
我正在使用
任何人都可以帮助我?
答案 0 :(得分:0)
我来看看JBuilder。它使您可以选择所需的属性。 github
答案 1 :(得分:-1)
看起来没有加载ProfileSerializer类。确保配置自动加载路径中包含app/serializers
。
<强> application.rb中强>
config.autoload_paths << Rails.root.join('app', 'serializers')