Rails:使用has_secure_password密码不能为空

时间:2017-07-04 07:10:43

标签: ruby-on-rails ruby passwords c9.io

我正在尝试按照本教程(Rails Tutorial by Michael Hartl)创建有效用户。填写密码字段仍然会给我错误"密码不能为空白"和#34;密码必须更长,谢谢6个字母"。我尝试通过rails控制台创建用户,这很好。只有当我尝试通过网站本身时才这样做。

我的用户模型如下所示:

class User < ApplicationRecord
  #before saving, make sure the email is in downcase
  before_save { self.email = email.downcase }

  validates :name,  presence: true, length: { maximum: 50 }
  VALID_EMAIL_REGEX = /\A[\w+\-.]+@[a-z\d\-.]+\.[a-z]+\z/i
  validates :email, presence: true, length: { maximum: 255 },
                format: { with: VALID_EMAIL_REGEX },
                uniqueness: {case_sensitive: false}

  has_secure_password
  validates :password, presence: true, length: { minimum: 6 }
end

我的user_controller看起来像这样:

class UsersController < ApplicationController
before_action :set_user, only: [:show, :edit, :update, :destroy]
#wrap_parameters :user, include: [:name, :email, :password, :password_confirmation]

def index
  @users = User.all
end

def show
  @user = User.find(params[:id])
  #debugger
end
def new
  @user = User.new
end

# GET /users/1/edit
def edit
end

#POST /users
#POST /users.json
def create
  @user = User.new(user_params) #params[:user])#user_params)

  respond_to do |format|
    if @user.save
      #flash[:success] = "Welcome to CourseBuddies!"

      format.html { redirect_to @user, notice: 'User was successfully created.' }
      format.json { render :show, status: :created, location: @user }
      redirect_to @user
    else
      #render 'new'
      format.html { render :new }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

# PATCH/PUT /users/1
# PATCH/PUT /users/1.json
def update
  respond_to do |format|
    if @user.update(user_params)
      format.html { redirect_to @user, notice: 'User was successfully updated.' }
      format.json { render :show, status: :ok, location: @user }
    else
      format.html { render :edit }
      format.json { render json: @user.errors, status: :unprocessable_entity }
    end
  end
end

# DELETE /users/1
# DELETE /users/1.json
def destroy
  @user.destroy
  respond_to do |format|
    format.html { redirect_to users_url, notice: 'User was successfully destroyed.' }
    format.json { head :no_content }
  end
end

private
  # Use callbacks to share common setup or constraints between actions.
  def set_user
    @user = User.find(params[:id])
  end

  # Never trust parameters from the scary internet, only allow the white list through.
  def user_params
    params.require(:user).permit(:name, :email, :pasword, :password_confirmation)
  end
end

这是我的迁移文件create_user:

class CreateUsers < ActiveRecord::Migration[5.0]
 def change
   create_table :users do |t|
     t.string :name
     t.string :email
     t.string :password
     t.string :password_confirmation

     t.timestamps
   end
 end

我尝试过在线发现的不同解决方案,例如使用&#34; wrap_parameters&#34;而且它似乎对我没有任何帮助。

My Gemfile如下所示:

# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.1'
gem 'bootstrap-sass', '3.3.7'
# make pw digest "has_secure_password" use a stateofart hash function (bcrypt)
gem 'bcrypt', '3.1.11'
# Use postgresql as the database for Active Record
gem 'pg', '~> 0.18.4'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby

# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more:     https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'

# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development

group :development, :test do
  gem 'sqlite3', '1.3.13' #railsTuto said to get this, but already use postgresql
  # Call 'byebug' anywhere in the code to stop execution and get a debugger console
  gem 'byebug', platform: :mri
end

group :development do
  # Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
  gem 'web-console', '>= 3.3.0'
  gem 'listen', '~> 3.0.5'
  # Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
  gem 'spring'
  gem 'spring-watcher-listen', '~> 2.0.0'
end

group :test do
  gem 'rails-controller-testing', '1.0.2'
  gem 'minitest-reporters',       '1.1.14'
  gem 'guard',                    '2.13.0'
  gem 'guard-minitest',           '2.4.4'
end

group :production do
  gem 'pg', '~>0.18.4'
  gem 'rails_12factor'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]

编辑:我的表格

<%- #FIELDS FOR THE FORM %>
<div class="row">
  <div class="col-md-6 col-md-offset-3">
    <%= form_for(@user, url: signup_path) do |f| %>

    <%- #ERROR MESSAGES %>
    <%= render 'shared/error_messages' %>

    <%= f.label :name %>
    <%= f.text_field :name, class: 'form-control' %>

    <br>
    <%= f.label :email %>
    <%= f.email_field :email, class: 'form-control' %>

    <br>  
    <%= f.label :password %>
    <%= f.password_field :password, class: 'form-control' %>

    <br>  
    <%= f.label :password_confirmation, "Confirmation" %>
    <%= f.password_field :password_confirmation, class: 'form-control' %>


    <br><br>
    <%= f.submit "Create my account", class: "btn btn-primary" %>
  <% end %>
</div>

我希望得到任何帮助或澄清。谢谢!

3 个答案:

答案 0 :(得分:3)

错误的实际原因是您password中的错误输入 pasworduser_params。修复拼写错误可以解决您的问题。

def user_params
  params.require(:user).permit(:name, :email, :password, :password_confirmation)
end

答案 1 :(得分:0)

我认为您需要在验证中添加allow_nil:true

validates :password, presence: true, length: { minimum: 6 }, allow_nil: true

as allow_nil:true将不允许创建空字符串。

答案 2 :(得分:0)

SOLUTION:

Okey,所以在我的控制器中修正了我的拼写错误(由@Pavan指出)之后,我也删除了在我的用户模型(@Divya Sharma)中的存在:true,所以它看起来像这样:

validates :password, length: { minimum: 6 }, allow_nil: true

我现在可以正确注册我的用户并登录!

我认为设置“allow_nil:true”实际上只是让我注册,因为不知怎的,我的password_fields仍然是“零”。我想这是因为我的错字错误。无论如何,谢谢你们这么快!