ruby-on-rails – 更新用户配置文件,而无需每次都输入密码和确认
发布时间:2020-12-17 02:01:53 所属栏目:百科 来源:网络整理
导读:我正在尝试为我的User模型创建一个名为edit_profile的新页面,以便用户可以编辑他的配置文件(字符串).我跟着 http://railscasts.com/episodes/41-conditional-validations 这是表单(edit_profile.html.erb): %= form_for @user,:html = { :multipart = true
|
我正在尝试为我的User模型创建一个名为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视图时,即使在我尝试提交新配置文件之前,我仍然收到此消息密码太短(最少6个字符). 这是我的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
当我只想在用户模型中编辑我的配置文件属性时,如何绕过密码验证? 谢谢! 其他相关信息: user.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,:email_format => true,:uniqueness => { :case_sensitive => false }
validates :password,#:presence => true,#:confirmation => true,:length => { :within => 6..40 }
validates :profile,:length => { :maximum => 160 }
end
解决方法
鉴于您正在讨论配置文件编辑,即您已经拥有密码的注册用户,您可能希望有条件地跳过密码更新.
如果password_confirmation为空,只需删除密码和password_confirmation.即: class UsersController < ApplicationController
before_filter :skip_password_attribute,only: :update
...
def update
...
end
private
def skip_password_attribute
if params[:password].blank? && params[:password_validation].blank?
params.except!(:password,:password_validation)
end
end
end
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
热点阅读
