ruby-on-rails-3 – Ruby on Rails更新设计用户属性而无需密码
发布时间:2020-12-17 02:16:47 所属栏目:百科 来源:网络整理
导读:我的应用程序中有两类帐户.我正在尝试更新其中一个(投资者)的属性.它们是通过设计使用一种形式创建的.因此,当投资者试图更新其帐户信息时,表单要求他们提供并确认其密码.我希望他们能够编辑(名字,姓氏等),而不必输入他们的密码,除非他们想要用这些字段更改他
我的应用程序中有两类帐户.我正在尝试更新其中一个(投资者)的属性.它们是通过设计使用一种形式创建的.因此,当投资者试图更新其帐户信息时,表单要求他们提供并确认其密码.我希望他们能够编辑(名字,姓氏等),而不必输入他们的密码,除非他们想要用这些字段更改他们的密码.
这是我在Investor控制器中的更新方法 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 repo的建议,但我无法让它为我工作. 如果您有任何建议或者我遗失了什么,请告诉我! 解决方法
你在params的任何地方发送:current_password吗? (即使它是空白的?)正常设计要求你在需要密码时使用
#update_with_password,所以我不确定你为什么要这样:update_attributes.
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |