如何添加用户名字段来设计宝石? [英] How to add username field to devise gem?
问题描述
这是我试过的,
-
rails g migration add_username_to_hrs
bundle exec rake db:migrate
添加了 attr_accessible:username
重新启动服务器
我的add_username_to_hr.rb
class AddUsernameToAuthorize < ActiveRecord::Migration
def change
add_column :authorizes, :username, :string
end
end
错误
未定义的方法`username' / p>
undefined method `username' for #
问题:如何在我的设计宝石中添加用户名字段?
Question: How can I add a username field in my devise gem?
推荐答案
我已经做了同样的事情。请按照下列步骤操作:
I have done the same. Please follow these steps:
-
rails生成迁移add_username_to_users用户名:string:uniq
rake db:migrate
在application_controller.rb中添加 attr_accessible:username
add attr_accessible :username
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }
devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:login, :username, :email, :password, :remember_me) }
devise_parameter_sanitizer.for(:account_update) { |u| u.permit(:username, :email, :password, :password_confirmation, :current_password) }
end
在配置/初始化设置中,如果要将电子邮件
替换为 usernname
in config/initializers if you want to replace email
by usernname
config.authentication_keys = [ :username ]
config.case_insensitive_keys = [ :username ]
config.strip_whitespace_keys = [ :username ]
更新视图。
update the views.
注意如果 attr_accessible:username
给出错误尝试 attr_accessor:username
这篇关于如何添加用户名字段来设计宝石?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!