添加用户名到Devise

时间:2018-01-13 02:14:41

标签: ruby-on-rails ruby devise account

我跑了" rails g migration add_username_to_users username:string"和" rake db:migrate"并将其添加到我的application_controller

before_action :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

但是当我尝试加载设计视图时出现此错误 Devise中的NoMethodError :: RegistrationsController #new 未定义的方法`for'为#你的意思?叉子 提取的来源(第7行)

第7行是:     devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation, :remember_me) }

更新

现在我收到了不同的错误

Devise中的NoMethodError ::注册#new

 <%= f.username_field :username, :autofocus => true, autocomplete: "username" %>

更新2 现在注册页面加载,但是当我按下注册按钮时,我收到此错误 Devise中的LoadError :: RegistrationsController #create 无法加载此类文件 - bcrypt_ext

 load_dependency(file) { result = super }

2 个答案:

答案 0 :(得分:0)

您的使用方法有误,这是正确的devise_parameter_sanitizer.permit(:sign_up, keys: [:subscribe_newsletter])。这是文档https://github.com/plataformatec/devise#strong-parameters

答案 1 :(得分:0)

尝试以下

// Setting the Order ID as a global variable add_action('woocommerce_email_before_order_table', 'email_order_id_as_a_global', 1, 4); function email_order_id_as_a_global($order, $sent_to_admin, $plain_text, $email){ $GLOBALS['order_id_str'] = $order->get_id(); } // Conditionally customizing footer email text add_action( 'woocommerce_email_footer_text', 'custom_email_footer_text', 10, 1 ); function custom_email_footer_text( $get_option ){ // Getting the email Order ID global variable $refNameGlobalsVar = $GLOBALS; $order_id = $refNameGlobalsVar['order_id_str']; // If empty email Order ID we exit if( empty($order_id) ) return; //Get the customer ID $user_id = get_post_meta( $order_id, '_customer_user', true ); // Get the user data $user_data = get_userdata( $user_id ); if ( in_array( 'wholesale_customer', $user_data->roles ) ) { $get_option = 'Store 1'; } else { $get_option = 'Store 2'; } return $get_option; }

application_controller.rb

现在允许您使用before_action :configure_permitted_parameters, if: :devise_controller? protected def configure_permitted_parameters devise_parameter_sanitizer.permit(:sign_up, keys: [:email, :username, :password, :password_confirmation]) devise_parameter_sanitizer.permit(:sign_in, keys: [:login, :username, :email, :password, :remember_me]) devise_parameter_sanitizer.permit(:account_update, keys: [:username, :email, :password, :password_confirmation, :current_password]) end 之类的额外列密钥,以便更多点击here

现在,username默认使用devise进行密码加密,您需要安装bcrypt密码加密选项,如下所示

bcrypt

希望能提供帮助