Rails 4.2 + ActiveModel Serializer 0.8.3

时间:2015-03-25 05:06:39

标签: ruby-on-rails-4 active-model-serializers

我有这些文件,我可以让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

任何人都可以帮助我?

2 个答案:

答案 0 :(得分:0)

我来看看JBuilder。它使您可以选择所需的属性。 github

答案 1 :(得分:-1)

看起来没有加载ProfileSerializer类。确保配置自动加载路径中包含app/serializers

<强> application.rb中

config.autoload_paths << Rails.root.join('app', 'serializers')