添加用户名以设计 rails 4

问题描述:

我有一个使用 devise 的 rails 4 应用程序.我正在尝试允许用户拥有与其关联的用户名.

I have a rails 4 application using devise. I'm trying to allow users to have a username associated with them.

我在用户表中添加了一个用户名(字符串)列,并且我的应用程序控制器如下所示:

I've added a username (string) column to the Users table, and had my Application controller look like this:

class ApplicationController < ActionController::Base

    before_filter :configure_permitted_parameters, if: :devise_controller?
    protect_from_forgery with: :exception
    protected
    def configure_permitted_parameters
       devise_parameter_sanitizer.for(:sign_in) { |u| u.permit(:username, :email) }
    end

end

而且我还在 users/sign_up 页面上为用户名添加了一个字段.

and I've also added a field for the username on the users/sign_up page.

但我收到此错误:

undefined local variable or method `devise_parameter_sanitizer' for #<Devise::RegistrationsController:0x00000101378a28>

所以基本上我的问题是为什么会出现这个错误,或者我怎样才能让用户获得用户名?

So basically my question is why is this error appearing, or how else can I get a user to get a username?

感谢大家的帮助!

您只允许在登录方法中使用用户名,但我假设当您创建新用户时,它在注册中方法.所以试试这个:

You're only permitting the username to be allowed on the sign in method, but I'm assuming when you create a new user, it's on the sign up method. So try this:

def configure_permitted_parameters
   devise_parameter_sanitizer.for(:sign_up) { |u| u.permit(:username, :email, :password, :password_confirmation) }
end

来源:https://github.com/plataformatec/devise#strong-parameters