Ruby on Rails更新无需密码即可设计用户属性

Ruby on Rails updating devise user attributes without password

我的应用程序中有两类帐户。我正在尝试更新其中之一(投资者)的属性。它们是通过设计使用一种形式创建的。因此,当投资者尝试更新其帐户信息时,该表格要求他们提供并确认密码。我希望他们能够编辑(名字,姓氏等)而无需输入密码,除非他们想在这些字段中更改密码。

这是我在"投资者"控制器中的更新方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 def update

    session[:investor_params] ||= {}
    session[:investor_params].deep_merge!(params[:investor]) if params[:investor].present?

    @investor.attributes = session[:investor_params]

    params[:investor_params].delete(:password) if params[:investor_params][:password].blank?
    params[:investor_params].delete(:password_confirmation) if params[:investor_params][:password_confirmation].blank?
    respond_to do |format|
    if @investor.update_attributes(params[:investor_params])
      session[:investor_params] = nil
      sign_in(@investor.account, :bypass => true)
      format.html { redirect_to(projects_url, :notice => 'Investor was successfully updated.') }
      format.xml  { render :xml => @investor, :status => :created, :location => @investor }

    else
      format.html { render :action =>"edit", :layout =>"investor" }
      format.xml  { render :xml => @investor.errors, :status => :unprocessable_entity }
    end
    end
  end

这是我的注册管理员

1
2
3
4
5
6
7
8
9
10
class RegistrationsController < Devise::RegistrationsController

  layout"index"

  protected

  def after_sign_up_path_for(resource)
    new_user_url(:account => resource)
  end
end

这是我的路线。rb

1
2
3
4
5
6
  devise_for :accounts, :controllers => {
    :registrations => 'registrations',
    :passwords     => 'passwords',
    :sessions      => 'sessions' }
  devise_scope :account do
    root :to => 'registrations#new'

这是我的投资者模型的一部分,引用了帐户模型的属性。

1
2
3
4
5
class Investor < ActiveRecord::Base

  has_one :account, :as => :profile

  accepts_nested_attributes_for :account

这是帐户模型中引用的部分

1
2
3
4
5
6
7
8
class Account < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :token_authenticatable, :encryptable, :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable,
         :recoverable, :rememberable, :trackable, :validatable

  # Setup accessible (or protected) attributes for your model
  attr_accessible :email, :password, :password_confirmation, :remember_me, :invited_by, :invited_by_id

我尝试了关于devise github repo的建议,但是我无法让它为我工作。

如果您有任何建议或如果我有任何遗漏,请让我知道!


您是否在参数的任何地方发送了:current_password? (即使它为空?)通常,devise希望您在需要密码时使用#update_with_password,所以我不确定为什么要通过:update_attributes来获取它。