Ruby on Rails教程:如何在不确认密码的情况下编辑用户信息 [英] Ruby on Rails Tutorial: How to edit user information without confirming the password

查看:81
本文介绍了Ruby on Rails教程:如何在不确认密码的情况下编辑用户信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Michael Hartl编写的Ruby on Rails教程.当前,为了编辑任何用户"属性,用户必须确认其密码.是否有任何方法可以不必更新而更新用户属性?

I've been working through the Ruby on Rails Tutorial by Michael Hartl. Currently, in order to edit any of the User attributes, the user must confirm their password. Is there any way to update the user attributes without having to do this?

我的表单如下:

    <%= form_for @user do |f| %>
      <div class="field">
        <%= f.label :course1 %><br />
        <%= f.text_field :course1 %>
      </div>
      <div class="actions">
        <%= f.submit "Update" %>
      </div>
    <% end %>"

和我在users_controller.rb中的更新定义如下:

and my update definition in users_controller.rb looks like this:

def update

    if @user.update_attributes(params[:user])
        flash[:success] = "Edit Successful."
        redirect_to @user
    else
        @title = "Edit user"
        render 'edit'
    end
end

当前,update_attributes操作失败.

Currently, the update_attributes action fails.

谢谢!

推荐答案

在您的User模型上,您可能会有类似的东西:

On your User model, you probably have something along the lines of:

validates_presence_of :password_confirmation

按如下所示添加if子句,这样,它仅在实际更改密码时才检查确认:

Add an if clause as follows, that way it only checks for the confirmation when the password is actually being changed:

validates_presence_of :password_confirmation, :if => :password_changed?

这篇关于Ruby on Rails教程:如何在不确认密码的情况下编辑用户信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆