内容简介:翻译自:https://stackoverflow.com/questions/39632699/rails-5-model-objects-not-showing-all-devise-attributes
我正在使用Rails 5 API和设计.我有一个用户模型. Schema看起来像这样.
create_table "users", force: :cascade do |t|
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.inet "current_sign_in_ip"
t.inet "last_sign_in_ip"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true, using: :btree
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true, using: :btree
end
但我遇到的问题是Rails只显示:id,:email,:created_at,:updated_at属性作为模型的一部分.例如,在rails控制台中
User.new => #<User id: nil, email: "", created_at: nil, updated_at: nil> User.first User Load (0.5ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT $1 [["LIMIT", 1]] => #<User id: 2, email: "your@email.com", created_at: "2016-09-22 03:58:04", updated_at: "2016-09-22 04:54:41">
但是这些属性存在于数据库中.这个问题在前面提到过. Devise with rails 5 但那里没有答案.请帮忙.
Devise限制了像encrypted_password这样的属性,因此关键信息不会在API调用中暴露出来.因此,要覆盖它,您需要覆盖serializable_hash方法.
def serializable_hash(options = nil) super(options).merge(encrypted_password: encrypted_password) end
这不是Rails 5特有的功能,而是用于保护属性的Devise功能.
希望有所帮助!
翻译自:https://stackoverflow.com/questions/39632699/rails-5-model-objects-not-showing-all-devise-attributes
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网
本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。
区块链核心算法解析
【瑞士】Roger Wattenhofer(罗格.瓦唐霍费尔) / 陈晋川、薛云志、林强、祝庆 / 电子工业出版社 / 2017-8 / 59.00
《区块链核心算法解析》介绍了构建容错的分布式系统所需的基础技术,以及一系列允许容错的协议和算法,并且讨论一些实现了这些技术的实际系统。 《区块链核心算法解析》中的主要概念将独立成章。每一章都以一个小故事开始,从而引出该章节的内容。算法、协议和定义都将以形式化的方式描述,以便于读者理解如何实现。部分结论会在定理中予以证明,这样读者就可以明白为什么这些概念或算法是正确的,并且理解它们可以确保实现......一起来看看 《区块链核心算法解析》 这本书的介绍吧!