Rails:在 Rails 模型中组合姓氏和名字
最终解决方案:
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我真诚地、诚实地、真正地赞扬您使用 Ruby 的强大功能,但由于这是两个字符串的直接静态连接,所以我会同意:
至于
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:
As for the
nil:NilClass
issue, are you adding first_name and last_name columns to your users table in your associated migration?简短说明
我认为在注册时获得
first_name
和last_name
是件好事:因此我们将对其进行广告验证。另外,验证长度并将其与一些正则表达式匹配也是个好主意。然后,只要登录只创建一次,我们将添加
before_create
回调,该回调仅在对象创建(而不是更新)时执行。仅当验证通过时才会运行before_create
回调,因此如果first_name
或last_name
为空 - 验证将不会通过,回调也不会”直到first_name
和last_name
填满后才执行。UPD
好的,就您收到的错误而言:
Short explanation
I think it is good to get
first_name
andlast_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 iffirst_name
orlast_name
is blank - validation won't be passed and callback won't be executed tillfirst_name
andlast_name
is filled.UPD
Ok, as far as you get your error: