2013-07-26 51 views
5

如何在創建設計用戶時自定義JSON輸出?自定義在創建用戶帳戶時設計用戶JSON響應

### User.rb ### 
class User < ActiveRecord::Base 
    devise :database_authenticatable, 
     :registerable, ... 
    ... 
end 

### Routes.rb ### 
... 
devise_for :users, :controllers => {:registrations => "registrations"} 
... 

我有我的用戶表是祕密的一些額外的領域,但是當我通過JSON做一個用戶創造這樣他們得到的JSON響應返回:

$ curl -H "Content-Type: application/json" -d '{"user" : {"username":"someone","email":"[email protected]","password":"awesomepass"}}' -X POST http://localhost:3000/users.json 

返回:

{"user":{"secret_field_1":"some value","secret_field_2":"some value","created_at":"2013-07-25T21:24:50-05:00","email":"[email protected]","first_name":null,"id":3226,"last_name":null,"updated_at":"2013-07-25T21:24:50-05:00","username":"someone"}} 

我想隱藏這些祕密字段,但不知道如何自定義JSON響應。

我已經嘗試了標準的ActiveRecord串行:

class UserSerializer < ActiveModel::Serializer 
    attributes :id, :created_at, :updated_at, :email, :first_name, :last_name, :username 
end 

無濟於事,我因爲設計的猜測。

回答

-1

根據您使用該JSON所做的操作,只需從序列化程序中刪除不想要的屬性即可。

例如:

class UserSerializer < ActiveModel::Serializer 
    attributes :id, :email, :username 
end 

我相信,在你的情況,你只是想這樣做。

但你也有可能包括在特定的條件的屬性:

class PostSerializer < ActiveModel::Serializer 
    attributes :id, :title, :body, :author 

    def include_author? 
    current_user.admin? 
    end 
end 

最後,你可以重寫屬性,方法,返回你所需要的哈希:

class PersonSerializer < ActiveModel::Serializer 
    attributes :first_name, :last_name 

    def attributes 
    hash = super 
    if current_user.admin? 
     hash["ssn"] = object.ssn 
     hash["secret"] = object.mothers_maiden_name 
    end 
    hash 
    end 
end 

READMEActiveModel::Serializers欲瞭解更多信息。

+1

串行器不工作。序列化程序代碼不包含祕密字段,但JSON輸出仍然存在。 –

+0

你確定Rails正在瀏覽序列化程序嗎? 也許你應該嘗試將一個binding.pry放入該序列化程序中,以便查看會發生什麼。否則,請嘗試在github或其他地方上傳您的項目樣本和相應的Gemfile(.lock),我會看看它。 – Simon

8

我剛碰到同樣的問題。我沒有精確地指出爲什麼,但Devise的SessionsController中看起來像respond_with(在Devise 3.0和active_model_serializers 0.8.1上測試)不會觸發ActiveModel::Serializer

所以我在我的控制器推翻respond_with

class SessionsController < Devise::SessionsController 

    def respond_with(resource, opts = {}) 
    render json: resource # Triggers the appropriate serializer 
    end 

end 

但是,由於它是,在我RegistrationsController正與respond_with。在那裏,我需要做到以下幾點:

class RegistrationsController < Devise::RegistrationsController 

    respond_to :json 
end 
+0

要生成兩個客戶控制器,你做了什麼?另外,你的routes.rb文件是什麼樣的? –

0

只是一個猜測,但它聽起來像是Rails是沒有找到你的串行器並使用to_json()。您是否在模型中定義了active_model_serializer()

+0

如果序列化程序是UserSerializer,並且類名稱是User,它將自動檢測到。 – mattwindwer

2

我最近碰到了這個,覆蓋respond_with沒有解決這個問題。我結束了在user.rb覆蓋to_json像這樣:

def to_json(arg) 
    UserSerializer.new(self).to_json 
end 

不知道什麼額外的arg是,但似乎由色器件混入的一個要求。

我使用了以下內容:

  • 的Rails 4.2.0
  • 設計3.4.1
0

我有同樣的問題,下面是我如何解決它,很簡單。

所有這些傳遞active_model_serializers (0.9.5)

覆蓋設計註冊的方法,並在您的自定義操作:

def registration 
    //Some process, and you get a @user when registration is successful. 

    render :json => UserSerializer.new(@user) 
end 

如果你希望將一些參數傳遞給您的自定義序列化(令牌例如),你可以通過它在你的行動:

render :json => UserSerializer.new(@user).as_json({auth_token: your_token}) 

而在你的串行器,只需使用:

class UserSerializer < ActiveModel::Serializer 
    attributes :id, :name, :avatar_url, :auth_token 

    def auth_token 
    serialization_options[:auth_token] 
    end 
end 
相關問題