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

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

内容简介:翻译自: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


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

查看所有标签

猜你喜欢:

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

零成本实现Web性能测试

零成本实现Web性能测试

温素剑 / 电子工业出版社 / 2012-2 / 59.00元

《零成本实现Web性能测试:基于Apache JMeter》是一本关于Web性能测试的实战书籍,读者朋友们在认真阅读完《零成本实现Web性能测试:基于Apache JMeter》后,相信能够将所学知识应用到生产实践中。《零成本实现Web性能测试:基于Apache JMeter》首先介绍基础的性能测试理论,接着详细介绍如何使用JMeter完成各种类型的性能测试。实战章节中作者以测试某大型保险公司电话......一起来看看 《零成本实现Web性能测试》 这本书的介绍吧!

MD5 加密
MD5 加密

MD5 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具