用户编辑个人资料表格不起作用

发布于 2025-01-03 20:03:49 字数 2738 浏览 0 评论 0原文

我的用户设置页面上有 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

非常感谢任何帮助!

I have 2 forms on my user settings page, one for all basic settings, and one for the profile picture. Every time I try to update a users photo I get an error saying "passwords can't be blank" even though the password fields are in a different form.

The code for the forms:

<%= 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 %>

and my user.rb file:

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

Any help is greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

追风人 2025-01-10 20:03:49

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

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

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

You then need to check if the password field coming blank then ignore the validation and if user fills anything in it, it should be checked and if it's on new record creation, it should be checked always.

So, I would say, it should go like this:

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

also update the method encrypt_password, just add this initial code

def encrypt_password
  return if password.to_s.empty?
  ...
  ... existing code
  ... 
end
若能看破又如何 2025-01-10 20:03:49

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

添加 :on => 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

The problem is with your presence validation of your virtual password attribute.

Adding an :on => create will stop the validation from firing when you are updating the user.

Try

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

A nice rails cast is here:
http://railscasts.com/episodes/250-authentication-from-scratch

对岸观火 2025-01-10 20:03:49

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

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

simply editing the password validation with the following should work:

validates :password, :presence     => true,
                                       :on => :create,
                                       :confirmation => true,
                                       :length       => { :within => 6..40 }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文