添加用户名以设计 rails 4 [英] Adding Username to devise rails 4

查看:27
本文介绍了添加用户名以设计 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

这篇关于添加用户名以设计 rails 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆