首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >用户编辑配置文件表单不起作用

用户编辑配置文件表单不起作用
EN

Stack Overflow用户
提问于 2012-02-09 09:47:41
回答 3查看 452关注 0票数 0

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

表单的代码:

代码语言:javascript
运行
复制
<%= 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文件:

代码语言:javascript
运行
复制
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

任何帮助都是非常感谢的!

EN

Stack Overflow用户

发布于 2012-02-09 10:12:43

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

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

试一试

代码语言:javascript
运行
复制
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

票数 1
EN
查看全部 3 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9204334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档