设计隐藏字段

编程入门 行业动态 更新时间:2024-10-28 16:30:24
本文介绍了设计隐藏字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我创建了一个设计模型

迁移是:

t.string :provider, null: false, default: "email" t.string :uid, null: false, default: "" ## Database authenticatable t.string :encrypted_password, null: false, default: "" ## Recoverable t.string :reset_password_token t.datetime :reset_password_sent_at # etc...

当我执行 rails c 并键入 User 时,我会看到其列的巨大列表

When I do rails c and type User there I get a huge list of its columns

User(id: integer, provider: string, uid: string, encrypted_password: string, reset_password_token: string, reset_password_sent_at: datetime, remember_created_at: datetime, sign_in_count: integer, current_sign_in_at: datetime, last_sign_in_at: datetime, current_sign_in_ip: string, last_sign_in_ip: string, confirmation_token: string, confirmed_at: datetime, confirmation_sent_at: datetime, unconfirmed_email: string...

但是当我键入 u = User.find(1)时.它只有一些列 id,provider,uid,created_at,updated_at .

But when I type u = User.find(1). It has only some columns id, provider, uid, created_at, updated_at.

当我执行 u.to_json 时,我得到 {"id":1,"provider":"email","uid":"mymail@mail",名称:"名称,"姓:"姓,"电子邮件:" mymail@mail," created_at:" 2016-08-07T18:27:24.186Z," updated_at:" 2016-08-07T18:34:48.647Z} .

它会忽略诸如 confirmed_at , confirmation_sent_at 等字段...

It ignores fields like confirmed_at, confirmation_sent_at etc...

因此,我想在模型中添加更多隐藏"列.我该怎么办?

So, I want to add some more "hidden" columns in my model. How do I do it?

谢谢!

推荐答案

Devise具有 BLACKLIST_FOR_SERIALIZATION 常量.在源中检查它: github/plataformatec/devise/blob/master/lib/devise/models/authenticatable.rb#L59

Devise has BLACKLIST_FOR_SERIALIZATION constant for it. Check it in source: github/plataformatec/devise/blob/master/lib/devise/models/authenticatable.rb#L59

您可以使用以下行在 config/initializers/devise.rb 的Devise配置中隐藏序列化列,例如:

You can hide columns from serialization in your Devise config at config/initializers/devise.rb with line like:

Devise :: Models :: Authenticatable :: BLACKLIST_FOR_SERIALIZATION.concat [:column_1,:column2]

更多推荐

设计隐藏字段

本文发布于:2023-06-05 01:15:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/508842.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:字段

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!