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 

은 어떤 도움이 크게 감사합니다!

답변

1

가상 암호 속성이 현재 존재하는지 확인하는 데 문제가 있습니다.

:on => create을 추가하면 사용자를 업데이트 할 때 유효성 검사가 중지됩니다.

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

캐스트 좋은 레일

시도는 여기에 있습니다 :

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

좋은 생각하지만 그것은 작동하지 않았다는 세션이 작동하지 않습니다. – BTHarris

+0

죄송합니다 세션이 무슨 뜻입니까? – BigFive

+0

분명히 사용자가 페이지를 변경할 때마다 세션 암호가 확인됩니다. – BTHarris

1

간단하게 작동해야 다음에 암호 검증을 편집 암호 필드가 비어있는 경우 유효성 검사를 무시하고 사용자가 그 안에 아무것도 기입하지 않으면 검사해야하며 새 것인지 확인해야합니다 레코드 생성은 항상 검사해야합니다.

그래서

, 나는 같이 가야 말할 것입니다 :

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

관련 문제