Rails:在 Rails 模型中组合姓氏和名字

发布于 2024-11-03 14:49:39 字数 906 浏览 1 评论 0原文

最终解决方案:

我有一个 Rails 3 应用程序,它使用 Devise 来处理身份验证。在注册表单中,我有以下字段:

  <p><%= f.label :first_name %><br />
  <%= f.text_field :first_name %></p>

  <p><%= f.label :last_name %><br />
  <%= f.text_field :last_name %></p>

我需要将名字和姓氏大写,并将它们组合在用户模型中名为“登录”的数据库字段中(例如姓氏、名字)。这是完整的用户模型:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, 
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :login

  validates :first_name, :last_name, :email, :password, :password_confirmation, :presence => true

  before_create :create_login

  def create_login
    self.login = "#{last_name.capitalize}, #{first_name.capitalize}"
  end

end

谢谢。

FINAL SOLUTION:

I have a Rails 3 app that uses Devise to handle authentication. In the signup form i have the following fields:

  <p><%= f.label :first_name %><br />
  <%= f.text_field :first_name %></p>

  <p><%= f.label :last_name %><br />
  <%= f.text_field :last_name %></p>

I need to capitalize first and last names and combine them in the User model in a database field called 'login' (e.g. Lastname, Firstname). Here is the complete user model:

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, 
         :recoverable, :rememberable, :trackable, :validatable

  attr_accessible :first_name, :last_name, :email, :password, :password_confirmation, :remember_me, :login

  validates :first_name, :last_name, :email, :password, :password_confirmation, :presence => true

  before_create :create_login

  def create_login
    self.login = "#{last_name.capitalize}, #{first_name.capitalize}"
  end

end

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不打扰别人 2024-11-10 14:49:39

我真诚地、诚实地、真正地赞扬您使用 Ruby 的强大功能,但由于这是两个字符串的直接静态连接,所以我会同意:

def create_login
    login = "#{last_name.capitalize}, #{first_name.capitalize}"
end

至于 nil:NilClass 问题,您是否将first_name 和last_name 列添加到关联迁移中的users 表中?

I genuinely, honestly, and truly applaud your use of the power of Ruby, but since this is such a straightforward and static concatenation of two strings, I'd go with:

def create_login
    login = "#{last_name.capitalize}, #{first_name.capitalize}"
end

As for the nil:NilClass issue, are you adding first_name and last_name columns to your users table in your associated migration?

遥远的她 2024-11-10 14:49:39
before_create :create_login
validates :first_name, :presence => true
validates :last_name, :presence => true

def create_login
  login = [last_name, first_name].map(&:capitalize).join(", ")
end

简短说明

我认为在注册时获得 first_namelast_name 是件好事:因此我们将对其进行广告验证。另外,验证长度并将其与一些正则表达式匹配也是个好主意。

然后,只要登录只创建一次,我们将添加 before_create 回调,该回调仅在对象创建(而不是更新)时执行。仅当验证通过时才会运行 before_create 回调,因此如果 first_namelast_name 为空 - 验证将不会通过,回调也不会”直到 first_namelast_name 填满后才执行。

UPD

好的,就您收到的错误而言:

def create_login
  login = [last_name, first_name].compact.map(&:capitalize).join(", ")
end
before_create :create_login
validates :first_name, :presence => true
validates :last_name, :presence => true

def create_login
  login = [last_name, first_name].map(&:capitalize).join(", ")
end

Short explanation

I think it is good to get first_name and last_name on registration: so we will ad validation to it. Also it is good idea to validate length and match it with some regexp.

Then, as far as login is creates only once, we will add before_create callback, which will be executed only when object is creating (not updating). before_create callback will be run only if validation is passed, so if first_name or last_name is blank - validation won't be passed and callback won't be executed till first_name and last_name is filled.

UPD

Ok, as far as you get your error:

def create_login
  login = [last_name, first_name].compact.map(&:capitalize).join(", ")
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文