【问题标题】:How to show notice if user update account is not successfull( devise gem )?如果用户更新帐户不成功(设计 gem),如何显示通知?
【发布时间】:2012-09-26 08:06:07
【问题描述】:

我重写了一个小设计 gem 以允许用户根据情况更改有或没有帐户的一些细节。

我不想使用设计错误消息,因为我使用的是 jQuery 验证插件。

当存在一些设计错误时,我想显示一些通知错误,但我无法找到需要放置 flash[:error] 的位置。

这是我的更新操作:

def update

self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
# custom logic
if params[:user][:password].present?
  result = resource.update_with_password(params[resource_name])
else
  result = resource.update_without_password(params[resource_name])
end

# standart devise behaviour
if result
  if is_navigational_format?
    if resource.respond_to?(:pending_reconfirmation?) && resource.pending_reconfirmation?
      flash_key = :update_needs_confirmation
    end
    set_flash_message :notice, flash_key || :updated
  end
  sign_in resource_name, resource, :bypass => true
  respond_with resource, :location => after_update_path_for(resource)
 else
   if params[:user][:password].present?
    clean_up_passwords resource
    render "edit_user/edit_password"
    end
  end

那么,有人可以帮我解决这个问题吗?

【问题讨论】:

    标签: ruby-on-rails devise overriding


    【解决方案1】:

    如果您使用的是 Jquery 验证,您是否通过执行类似 <%= form_for @user, :validate => true, do |f|%> 的操作在您的视图上启用了此功能

    更新 你可以做一些类似的事情

    def update_password
        @user = current_user    
        @current_password = params[:user][:current_password]
        @password = params[:user][:password]
    
        if @user.valid_password?(@current_password)
          if @current_password == @password
            redirect_to user_path(@user)
            flash[:error] = "Current password cannot be the same as your new password."
          else
            if @user.update_attributes(params[:user])
              redirect_to login_path
              flash[:success] = "Password has been changed."
            else
              redirect_to user_path(@user)
              flash[:error] = "Didn't save contact an administrator."
            end
          end
        else
            redirect_to user_path(@user)
            flash[:error] = "Current password is incorrect."
        end
      end
    

    如果您将我建议的最高位添加到您的视图中并正确安装了 Jquery 验证,这应该可以工作

    【讨论】:

    • 我的意思是,当提交后提交失败时,例如当前密码错误,我想显示 flash[:error]。或者你知道我可以使用 jQuery 验证器检查当前密码吗?
    • DJ,对不起,您的代码没有显示任何通知。也许,我将使用 jQuery 验证插件检查当前密码?我已经在检查电子邮件的唯一性,你能帮我检查当前密码的方法吗?
    • 我将它插入到我的 edit_password 操作中,而不是在注册控制器的更新操作中。
    猜你喜欢
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 2013-09-23
    • 2013-09-26
    相关资源
    最近更新 更多