简体   繁体   English

用户名不能为空

[英]Username can't be blank

So I enter a username, email, and password (+password confirmation) and it once I hit submit, it says: "Username can't be blank" and I clearly entered a username.所以我输入了用户名、电子邮件和密码(+密码确认),一旦我点击提交,它就会说:“用户名不能为空”,我清楚地输入了一个用户名。

Here is my user.rb这是我的 user.rb

class User < ActiveRecord::Base
  validates :user_name, presence: true, length: { minimum: 4, maximum: 16

  }
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable
end


This is my registrations_controller:这是我的 registrations_controller:

class RegistrationsController < Devise::RegistrationsController

  private

  def sign_up_params
    params.require(:user).permit(:email, :user_name, :password, :password_confirmation)
  end

  def account_update_params
    params.require(:user).permit(:email, :user_name, :password, :password_confirmation, :current_password)
  end
end


And the following is my new.html.erb以下是我的 new.html.erb

<h2>Sign up</h2>

<%= simple_form_for(resource, as: resource_name, url: registration_path(resource_name)) do |f| %>
  <%= f.error_notification %>

  <div class="form-inputs">
    <%= f.input :user_name %>
    <%= f.input :email, required: true, autofocus: true %>
    <%= f.input :password, required: true, hint: ("#{@minimum_password_length} characters minimum" if @minimum_password_length) %>
    <%= f.input :password_confirmation, required: true %>
  </div>

  <div class="form-actions">
    <%= f.button :submit, "Sign up" %>
  </div>
<% end %>

<%= render "devise/shared/links" %>

add_username_to_users.rb (migration) add_username_to_users.rb(迁移)

class AddUsernameToUsers < ActiveRecord::Migration
  def change
    add_column :users, :username, :string
    add_index :users, :username, unique: true
  end
end

If there's any other needed to be posted, to find more about the error, please comment.如果还有其他需要发布的,要查找有关错误的更多信息,请发表评论。 Also, note that I have devise gem (properly installed) and I'm using a cloud 9 editor.另外,请注意我已经设计了 gem(正确安装)并且我使用的是 cloud 9 编辑器。 Thanks in advance!提前致谢!

When reviewing your code on c9.io there is definitely an issue with your migrations file where you have this:在 c9.io 上查看您的代码时,您的迁移文件肯定存在问题:

class CreateAddUserNameToUsers < ActiveRecord::Migration
  def change
    create_table :add_user_name_to_users do |t|
      t.string :user_name

      t.timestamps null: false
    end
  end
end

That actually created a table called add_user_name_to_users , which you can find in your schema.rb file.这实际上创建了一个名为add_user_name_to_users的表,您可以在schema.rb文件中找到它。 So when you are creating your User with the sign up form, there is no user_name to validate on the User 's table since it does not exist.因此,当您使用注册表单创建您的User时,没有user_name可以在User的表上进行验证,因为它不存在。 You should remove your migration file 20171231162016_create_add_user_name_to_users.rb since it is not what you are looking to do or drop the table ( here are some docs for changing your migrations).你应该删除你的迁移文件20171231162016_create_add_user_name_to_users.rb因为它不是你想要做的或者删除表( 这里有一些用于更改迁移的文档)。

Instead make sure you follow this devise documentation on adding a user_name as a sign up/ sign in parameter here .相反,请确保您遵循有关在此处添加user_name作为注册/登录参数的设计文档。

You also have a models table that has the same fields as your users table (also assuming that this was put here by accident. Make sure you double check your migrations as it looks like that was one of the results of the error you are seeing.您还有一个models表,该表与您的users表具有相同的字段(也假设这是偶然放在这里的。确保您仔细检查您的迁移,因为它看起来像是您所看到的错误的结果之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM