Rails 4.1"密码不能为空白" ...再次

时间:2014-07-15 18:09:51

标签: ruby-on-rails-4.1

我正在重新开始进行RoR开发,所以我正在努力追赶4.X.我正在使用RVM运行Rails 4.1,Ruby 2.1.1。我已经多次看到这个问题并实现了我找到的所有解决方案,但仍然收到“密码不能为空”的消息。以下是我的代码。我希望有人看到这个问题并知道答案。

==============

class RegisterController < ApplicationController

def signup
  @user = User.new()
end


def create
  @user = User.new(user_params)
  if @user.save
   redirect_to :action => "signup_success"
  else
   render :action => "signup"
  end
end


def signup_success
end


private
    def user_params
       params.require(:user).permit(:firstname, :lastname, :screenname, :email, :postal_code, :password,:password_confirmation)
   end
end

==========

require 'digest/sha2'

class User < ActiveRecord::Base

attr_accessor :password, :password_confirmation

def password=(pass)
    unless pass.blank?
        salt = [Array.new(6){rand(256).chr}.join].pack("m").chomp
        self.password_salt, self.password_hash = salt, Digest::SHA2.hexdigest(pass + salt)
end
end

end

============

<div class='row'>
    <h6>Sign-Up NOW!</h6>
    <div class='c5'>
    <%= form_for @user, as: :user, method: :post, url: register_create_path(@user), :html => {:class => 'hform'}  do |f| %>

        <%= f.label 'firstname','First name'%><%= f.text_field :firstname, :required => true , :class => 'required'%>
        <%= f.label 'lastname','Last name' %> <%= f.text_field :lastname, :required => true, :class => 'required' %>
        <%= f.label 'email','Email'%> <%= f.text_field :email, :required => true, :class => 'required'%>
        <label>Postal Code</label><%= f.text_field :postal_code, :required => true, :class => 'required' %>
        <hr>
        <%= f.label 'screenname','Username'%><%= f.text_field :screenname, :required => true, :class => 'required'  %>
        <%= f.label 'password', 'Password'%> <%= f.password_field 'password', :required => true, :class => 'required' %>
        <%= f.label 'password_confirmation','Confirm Password'%><%= f.password_field 'password_confirmation', :required => true, :class => 'required'%>
        <%= hidden_field_tag :authenticity_token, form_authenticity_token -%>
        <p class='text-center'><%=f.submit "Create Account"%></p>

    <%end%>
</div>

</div>

===========

2 个答案:

答案 0 :(得分:0)

已解决的问题

我从用户模型中删除了Bcrypt和“has_secure_password”。这解决了我的问题。我更新了下面的代码,以防有人遇到同样的问题。“password”和“password_confirmation”在提交表单数据时被过滤。

答案 1 :(得分:0)

'has_secure_password'包含一些验证。你只需要在它之后添加一个选项,如下面的选项。

has_secure_password validataions: false