【发布时间】:2014-07-03 15:15:52
【问题描述】:
我有一个 _form.html.erb 部分,它将用户输入发送到控制器中的更新方法
<div id='myaccount-profile_name_form' class='clearfix'>
<fieldset id="myaccount-profile" class='five columns'>
<ul id="posting_classified_form">
<li>
<%= f.label :first_name %>
<%= f.text_field :first_name, :placeholder => "First Name" %>
</li>
<li>
<%= f.label :last_name %>
<%= f.text_field :last_name %>
</li>
<li>
<%= f.label :email %>
<%= f.text_field :email %>
</li>
<li>
<%= f.label :form_birth_date, "Birthday" %>
<%= f.text_field :form_birth_date, :class => 'ui-yearpicker', :placeholder => "mm/dd/yyyy" %>
</li>
</ul>
</fieldset>
</div>
<div id='myaccount-profile_form' class='clearfix' >
<fieldset id="myaccount-profile-password" class='five '>
<ul id="posting_classified_form">
<li>
<%= f.label :current_password, "Current Password", :placeholder => "We need your current password to confirm your changes", :required => true %>
<%= f.password_field :password %>
</li>
<li>
<%= f.label :password, "New Password" %>
<%= f.password_field :password %>
</li>
<li>
<%= f.label :password_confirmation, "Confirm", :style => "white-space:normal;" %>
<%= f.password_field :password_confirmation %>
</li>
</ul>
</fieldset>
<div class="actions"><%= f.submit 'Update' , :class => 'goButton', :style => 'width:auto;float:right;'%></div>
</div>
这是控制器中的编辑/更新方法
def edit
@user = current_user
end
def update
@user = current_user
if @user.valid_password?(params[:user][:current_password])
binding.pry
@user.update_attributes(params[:user])
flash[:notice] = "Successfully updated user."
redirect_to umarket_index_url
else
flash[:notice] = "An error occurred while updating user please try again."
redirect_to umarket_index_url
# render :edit
end
end
我们正在使用 authlogic gem 进行身份验证。我找到了valid_password?来自 authlogic 文档。我想让用户只有在用户输入正确的密码后才能更改密码。
服务器日志如下所示
Started GET "/myaccount/overview/edit" for 127.0.0.1 at 2014-07-03 11:16:07 -0400
Processing by Myaccount::OverviewsController#edit as HTML
User Load (0.5ms) SELECT "users".* FROM "users" WHERE "users"."id" = 6 LIMIT 1
Rendered myaccount/overviews/_form.html.erb (2.7ms)
Rendered myaccount/overviews/edit.html.erb (5.3ms)
Completed 200 OK in 16ms (Views: 5.9ms | ActiveRecord: 0.5ms)
Started PUT "/myaccount/overview" for 127.0.0.1 at 2014-07-03 11:16:14 -0400
Processing by Myaccount::OverviewsController#update as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"b01gMlAQOJpu6Zu+mg+M4v2VYfm0AZvTAfKXa+ArJxU=", "user"=>{"first_name"=>"Judy", "last_name"=>"Ngai", "email"=>"judy.ngai1228@gmail.com", "form_birth_date"=>"spree@example.com", "password"=>"[FILTERED]", "password_confirmation"=>"[FILTERED]"}, "commit"=>"Update"}
User Load (0.6ms) SELECT "users".* FROM "users" WHERE "users"."id" = 6 LIMIT 1
Redirected to http://localhost:3000/umarket
Completed 302 Found in 15ms (ActiveRecord: 0.6ms)
密码未更改。日志中没有错误。我不使用 authlogic 所以我很困惑。
【问题讨论】:
-
在执行
@user.update_attributes(params[:user])后尝试查看@user.errors以查看是否有任何验证失败