ruby-on-rails – 没有显示所有Devise属性的Rails 5模型对象

栏目: Ruby · 发布时间: 5年前

内容简介:翻译自: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_pa​​ssword这样的属性,因此关键信息不会在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


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Effective STL中文版

Effective STL中文版

[美] Scott Meyers / 潘爱民、陈铭、邹开红 / 电子工业出版社 / 2013-5 / 59.00元

《Effective STL中文版:50条有效使用STL的经验》是EffectiveC++的第3卷,被评为“值得所有C++程序员阅读的C++书籍之一”。《Effective STL中文版:50条有效使用STL的经验》详细讲述了使用STL的50条指导原则,并提供了透彻的分析和深刻的实例,实用性极强,是C++程序员必备的基础书籍。C++的标准模板库(STL)是革命性的,要用好STL并不容易。《Effe......一起来看看 《Effective STL中文版》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

UNIX 时间戳转换
UNIX 时间戳转换

UNIX 时间戳转换