4
我在我的應用程序中有兩類帳戶。我正在嘗試更新其中一個(投資者)的屬性。它們是通過設計使用一種形式創建的。因此,當投資者嘗試更新其帳戶信息時,表格要求他們提供並確認其密碼。我希望他們能夠編輯(名字,姓氏等),而不必輸入他們的密碼,除非他們想用這些字段更改他們的密碼。Ruby on Rails更新設計無需密碼的用戶屬性
這裏是投資者控制器
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
這裏我更新的方法是我註冊控制器
class RegistrationsController < Devise::RegistrationsController
layout "index"
protected
def after_sign_up_path_for(resource)
new_user_url(:account => resource)
end
end
這裏是我的routes.rb
devise_for :accounts, :controllers => {
:registrations => 'registrations',
:passwords => 'passwords',
:sessions => 'sessions' }
devise_scope :account do
root :to => 'registrations#new'
這是一部分我的投資者模型引用了賬戶模型的屬性。
class Investor < ActiveRecord::Base
has_one :account, :as => :profile
accepts_nested_attributes_for :account
這裏是
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
我試圖在色器件GitHub庫的建議,但我無法得到它的工作對我來說是引用的帳戶模型的一部分。
如果您有任何建議,或者我缺少任何東西,請告訴我!
我不知道包含update_with_password方法的設備文件在哪裏;儘管設計作爲寶石安裝,你知道我在哪裏可以找到它嗎? – seanpat09 2012-01-09 20:34:31
如果您使用的是bundler/textmate,則可以使用命令行中的「mate $(bundle show devise)」來編輯gem的源代碼。否則它應該在你的$ GEM_HOME路徑中。在gem的源代碼中,您可以在以下位置查看該方法:lib/devise/models/database_authenticatable.rb 如果在更新時_want_需要密碼,則只需使用此方法。嘗試檢查,看看你是否意外傳遞參數[:current_password] – RobH 2012-01-09 20:39:19
非常感謝您的幫助,迄今爲止,我在這方面有點新。我實際上想更新而不需要密碼。我的表單包含所有可更新的字段,包括密碼和password_confirmation。我希望能夠更新其餘的信息,而無需在密碼和password_confirmation字段中輸入任何內容。當我這樣做時,我收到一條錯誤消息,說他們不能爲空。此外,:current_password在我們的項目文件中沒有定義,但它是在設計寶石源中定義的;你說的是什麼? – seanpat09 2012-01-09 21:41:27