我正在尝试为我的用户模型创建一个名为 edit_profile 的新页面,以便用户可以编辑他的个人资料(字符串)。我正在关注http://railscasts.com/episodes/41-conditional-validations
这是表格(edit_profile.html.erb):
<%= form_for @user, :html => { :multipart => true } do |f| %>
<div class="field">
<%= f.label :profile %><br/>
<%= f.text_area :profile, :class => "round" %><br />
</div>
<div class="actions">
<%= submit_tag "update", :id => "updateSubmit" %>
</div>
<% end %>
我遇到的问题是我验证了密码和密码确认的存在。当我加载我的 edit_profile 视图时,Password is too short (minimum is 6 characters)
即使在我尝试提交新的配置文件之前,我也会不断收到此消息。
这是我的 users_controller.rb:
def edit_profile
@user = current_user
@user.updating_password = false
@user.save
@title = "Edit profile"
end
def update
@user = User.find(params[:id])
if @user.update_attributes(params[:user])
flash[:success] = "Account updated."
redirect_to @user
else
@title = "Edit user"
render 'edit'
end
end
当我只想在用户模型中编辑我的个人资料属性时,如何绕过我的密码验证?
谢谢!
其他相关信息:
用户.rb
class User < ActiveRecord::Base
attr_accessor :password, :updating_password
attr_accessible :name, :email, :password, :password_confirmation, :photo,
:profile
before_save :downcase_fields
before_save :encrypt_password
validates_presence_of :password, :if => :should_validate_password?
validates_confirmation_of :password, :if => :should_validate_password?
def should_validate_password?
updating_password || new_record?
end
validates :name, :presence => true,
:name_format => true,
:uniqueness => { :case_sensitive => false }
validates :email, :presence => true,
:email_format => true,
:uniqueness => { :case_sensitive => false }
validates :password,
#:presence => true,
#:confirmation => true,
:length => { :within => 6..40 }
validates :profile, :length => { :maximum => 160 }
end