【发布时间】:2015-03-25 05:06:39
【问题描述】:
我有这些文件,我可以让 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}}
我正在使用
- Rails 4.2.0
- active_model_serializer 0.8.3
谁能帮帮我?
【问题讨论】:
-
你现在得到什么格式?
-
如果我使用 render json: profile 它会返回一个完整的配置文件 json 对象:{id: 4, user_id: 4, name: "Alex" ... + 所有属性个人资料
标签: ruby-on-rails-4 active-model-serializers