【问题标题】:User edit Profile Form Not Working用户编辑个人资料表单不起作用
【发布时间】:2012-02-09 01:47:40
【问题描述】:

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

表格代码:

<%= 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+\-.]+@[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

非常感谢任何帮助!

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-3 ruby-on-rails-3.1 rails-activerecord erb


    【解决方案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
    

    【讨论】:

      【解决方案2】:

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

      添加:on =&gt; create 将在您更新用户时停止验证。

      试试

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

      这里有一个不错的 rails 演员表: http://railscasts.com/episodes/250-authentication-from-scratch

      【讨论】:

      • 抱歉,您所说的会话是什么意思?
      • 您能告诉我们您的 encrypt_password 方法和您的控制器操作吗? (基于对其他回复的评论)看起来好像您正在加密一个空密码并将其保存到数据库中,即使您没有更改密码。
      【解决方案3】:

      只需使用以下内容编辑密码验证即可:

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

      【讨论】:

      • 无法让它工作,当我使用编辑配置文件表单时,它会擦除​​密码,因此可以使用空白密码字段登录
      猜你喜欢
      • 2018-09-17
      • 2020-10-16
      • 2011-11-15
      • 2012-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-04-27
      • 1970-01-01
      相关资源
      最近更新 更多