' validates_confirmation_of:密码'失败,但"密码不能为空白"消息显示

时间:2015-01-10 05:12:01

标签: ruby-on-rails model form-helpers

我正在浏览Railscast on "authentication from scratch"并一步一步地跟进。

表单报告“密码不能为空”,即使错误应该是密码和password_confirmation不匹配。即当我输入密码并故意输入错误的确认信息时,显示的错误信息仍然是“密码不能为空”。

这就是我的用户模型:

class User < ActiveRecord::Base
    attr_accessor :password
    validates_confirmation_of :password
    validates_presence_of :password, :on => :create
    validates_presence_of :email 
    validates_uniqueness_of :email
end 

如果错误实际上与password和password_confirmation不匹配,我如何才能使错误消息显示“密码不匹配”或类似内容?

_form.html.erb partial

  <% if @user.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(@user.errors.count, "error") %> prohibited this user from being saved:</h2>

      <ul>
      <% @user.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= f.label :name %><br>
    <%= f.text_field :name %>
  </div>
  <div class="field">
    <%= f.label :email %><br>
    <%= f.text_field :email %>
  </div>
  <div class="field">
    <%= f.label :password %><br>
    <%= f.password_field :password %>
  </div>
  <div class="field">
    <%= f.label :password_confirmation %><br>
    <%= f.password_field :password_confirmation %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

用户控制器

class UsersController < ApplicationController
  before_action :set_user, only: [:show, :edit, :update, :destroy]

  # GET /users
  # GET /users.json
  def index
    @users = User.all
  end

  # GET /users/1
  # GET /users/1.json
  def show
  end

  # GET /users/new
  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)

    respond_to do |format|
      if @user.save
        format.html { redirect_to @user, notice: 'User was successfully created.' }
        format.json { render :show, status: :created, location: @user }
      else
        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, :pass)
    end
end

2 个答案:

答案 0 :(得分:2)

我认为问题在于以下代码。

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

请用以下方法替换上述方法。

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

如果您收到任何错误,请回复。

答案 1 :(得分:0)

验证是否存在确认而非密码。然后它应该工作。

class User < ActiveRecord::Base
    attr_accessor :password, :password_confirmation
    validates_confirmation_of :password
    validates_presence_of :password_confirmation, :on => :create
    validates_presence_of :email 
    validates_uniqueness_of :email
end

请参阅Rails Guides