2015-03-25 119 views
1

我有這些文件,我可以得到ActiveModel Serializer的工作原理。Rails 4.2 + ActiveModel Serializer 0.8.3

# 
# 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

任何人都可以幫到我嗎?

+0

什麼格式,你現在明白了嗎? – 2015-03-25 07:57:50

+0

如果我使用**呈現json:profile **,它將返回完整的個人資料json對象:{id:4,user_id:4,name:「Alex」... +個人資料的所有屬性 – edudepetris 2015-03-25 12:34:37

回答

-1

它看起來像ProfileSerializer類沒有被加載。確保app/serializers包含在config autoload路徑中。

application.rb中

config.autoload_paths << Rails.root.join('app', 'serializers') 
相關問題