0

我在我的用户设置页面上有两个表单,一个用于所有基本设置,另一个用于个人资料图片。每次尝试更新用户照片时,即使密码字段的格式不同,我也会收到一条错误消息,提示“密码不能为空”。用户编辑个人资料表格不工作

为形式的代码:

<%= form_for @user, :html=> { :multipart => true} do |f| %> 
    <%= render 'shared/error_messages', :object => f.object %> 
<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, "Confirmation" %><br /> 
    <%= f.password_field :password_confirmation %> 
    </div> 
    <div class="actions"> 
     <%= f.submit "Update" %> 
    </div> 
<% end %> 

    <%= form_for @user, :html=> { :multipart => true} do |f| %> 
<%= f.file_field :photo %> 
     <br /> 
    <%= f.submit "Update" %> 
    <% end %> 

和我user.rb文件:

class User < ActiveRecord::Base 

    attr_accessor :password 

    attr_accessible :name, :email, :password, :password_confirmation, :photo 

    has_attached_file :photo, 
        :styles => { 
        :thumb=> "50x50#", 
        :small => "220x220>" }, 
        :storage => :s3, 
        :s3_credentials => "#{Rails.root}/config/s3.yml", 
        :path => "/:style/:id/:filename" 

has_many :microposts, :dependent => :destroy 
has_many :relationships, :foreign_key => "follower_id", 
          :dependent => :destroy 
has_many :following, :through => :relationships, :source => :followed 
has_many :reverse_relationships, :foreign_key => "followed_id", 
            :class_name => "Relationship", 
            :dependent => :destroy 
has_many :followers, :through => :reverse_relationships, :source => :follower 

    email_regex = /\A[\w+\-.][email protected][a-z\d\-.]+\.[a-z]+\z/i 

    validates :name, :presence => true, 
       :length => { :maximum => 50 } 
    validates :email, :presence => true, 
       :format  => { :with => email_regex }, 
       :uniqueness => { :case_sensitive => false } 

    validates :password, :presence  => true, 
             :confirmation => true, 
             :length  => { :within => 6..40 } 

             before_save :encrypt_password 

任何帮助,不胜感激!

回答

2

然后,您需要检查密码字段空白,然后忽略验证,如果用户填充任何内容,则应该检查它是否为新的创建记录时,应该始终检查。

所以,我会说,它应该是这样的:

validates :password, :presence  => true, 
             :if => :validate_password?, 
             :confirmation => true, 
             :length  => { :within => 6..40 } 

def validate_password? 
    if new_record? 
    return true 
    else 
    if password.to_s.empty? 
     return false 
    else 
     return true 
    end 
    end 
end 

还更新方法ENCRYPT_PASSWORD,只要加入这个初始代码

def encrypt_password 
    return if password.to_s.empty? 
    ... 
    ... existing code 
    ... 
end 
+0

明白了!谢谢! – BTHarris 2012-02-11 18:42:25

1

问题是您的虚拟密码属性的存在验证。

添加一个:on => create将在更新用户时停止验证激发。

尝试

validates_length_of  :password, :length => { :within => 6..40 }, :allow_blank => true 
validates_confirmation_of :password 
validates_presence_of  :password, :on => :create 

投一个很好的轨道是在这里: http://railscasts.com/episodes/250-authentication-from-scratch

+0

好主意,但它没有工作,会议现在不工作。 – BTHarris 2012-02-09 03:09:14

+0

对不起,这是什么意思? – BigFive 2012-02-09 04:20:07

+0

显然每当用户更改页面时会话密码都会被验证。 – BTHarris 2012-02-09 04:27:03

1

简单的编辑用下面的密码验证应该工作:

validates :password, :presence  => true, 
             :on => :create, 
             :confirmation => true, 
             :length  => { :within => 6..40 } 
+0

无法让它工作,当我使用编辑配置文件形式它抹的密码,所以可以登录一个空白的密码字段 – BTHarris 2012-02-09 15:51:08

相关问题