Ruby on rails在Devise上编辑用户而不输入密码

时间:2012-11-23 23:04:34

标签: ruby-on-rails devise

我知道这个问题已经被问了好几次,而且每个答案都会导致设计维基,但我无法让它发挥作用。我在这里关注了维基和其他一些帖子,但没有运气。我想让用户更新他的信息(电子邮件。用户名。姓名等),而不必每次都输入密码。

我的registrations_controller.rb看起来像这样:

class RegistrationsController < Devise::RegistrationsController
  def update
    # required for settings form to submit when password is left blank
    if params[:user][:password].blank?
      params[:user].delete("password")
      params[:user].delete("password_confirmation")
    end

    @user = User.find(current_user.id)
    if @user.update_attributes(params[:user])
      # Sign in the user bypassing validation in case his password changed
      sign_in @user, :bypass => true
      redirect_to root_path
    else
      render "edit"
    end
  end
end

和我的routes.rb看起来像这样:

Sparechef::Application.routes.draw do
  get "users/profile"

  devise_for :users
  devise_for :controllers => { :registrations => "registrations" }
  resources :dashboard

  root to: "home#index"

  match "profile" => "users#profile"
end

我仍然收到消息“当前密码不能为空”。如果我更新我的路线以跟随devise_for :users, :controllers => { :registrations => "registrations" }设计wiki,我会收到此错误:

Can't mass-assign protected attributes: current_password

有人知道如何让它发挥作用吗?

1 个答案:

答案 0 :(得分:0)

根据上述问题评论中的@michael,在attr_accessible :current_password中添加User