如何在RoR中使用Devise时删除令牌认证用户的当前密码要求?

时间:2013-06-20 04:22:43

标签: ruby-on-rails authentication devise passwords

更新1:更新了问题陈述

问题陈述

我正在使用Devise并向注册用户提供邀请其他人访问该网站的选项;在这种情况下,我使用ActionMailer通过带有令牌认证的URL发送邀请(例如http://localhost:3000/payments?auth_token=SsdLxnQ9Eemf6mNsFDfu)。这些新用户具有属性non_registered = 1,可以访问某些需要身份验证的资料,而其他功能则不可用,因为它们是非注册的。我希望Users来到我的网站,在使用该网站后可以选择创建密码并成为完全注册的用户,但收到错误消息当前密码不能为空< / em>当他们编辑他们的帐户信息以创建新密码时。

我意识到这有点像初学者的问题,但我是初学者。爱的RoR和出现的每个问题都是一个学习机会。知道我的代码有什么问题吗?

我的进步

我环顾四周,发现了一些相关链接,但似乎没有一个解决我正在处理的具体用例:

我确实覆盖了Registrations控制器,并自定义了设计Edit视图以删除current_password字段。我还在:current_password模型中添加User attr_accessibleattr_accessor,但不确定是否有必要。无论如何,在尝试更新密码时,我仍然收到错误当前密码不能为空

我的代码

应用/控制器/ registrations_controller.rb

class RegistrationsController < Devise::RegistrationsController
    def update
        if params[:user][:not_registered] == "1"
            params[:user].delete("current_password")
        end
        successfully_updated = super
        if successfully_updated
            params[:user][:not_registered] == "0"
        end
    end

    def new
        super
    end    

    def create
        super
    end 

    def edit
        super
    end 

    def cancel
        super
    end 

    def destroy
        super
    end     
end

应用/视图/设计/注册/ edit.html.erb

<% if current_user.not_registered != 1 %>
  <h2>Edit <%= resource_name.to_s.humanize %></h2>
<% else %>
  <h2>Sign up</h2>
<% end %>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }) do |f| %>
  <%= devise_error_messages! %>

  <div><%= f.label :email %><br />
  <%= f.email_field :email, :autofocus => true %></div>

  <% if devise_mapping.confirmable? && resource.pending_reconfirmation? %>
    <div>Currently waiting confirmation for: <%= resource.unconfirmed_email %></div>
  <% end %>

  <div><%= f.label :password %><br />
    <%= f.password_field :password, :autocomplete => "off" %></div>

  <div><%= f.label :password_confirmation %><br />
  <%= f.password_field :password_confirmation %></div>

  <% if current_user.not_registered != 1 %>
    <div><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i><br />
    <%= f.password_field :current_password %></div>
  <% end %>

  <div class="field">
    <%= f.hidden_field :not_registered, :value => current_user.not_registered %>
  </div>

  <% if current_user.not_registered != 1 %>
    <div><%= f.submit "Update" %></div>
  <% else %>
    <div><%= f.submit "Sign up" %></div>
  <% end %>
<% end %>

应用/模型/ user.rb

class User < ActiveRecord::Base
  devise :database_authenticatable, :registerable, :token_authenticatable,
         :recoverable, :rememberable, :trackable, :validatable, :confirmable

  attr_accessible :email, :password, :password_confirmation,
  :remember_me, :not_registered, :pay_method, :pay_desc, :email_instructions, :current_password

  attr_accessor :current_password
  has_many :payments
end

1 个答案:

答案 0 :(得分:0)

终于想通了!我通过实现此处列出的代码解决了上述问题:

Ruby on Rails, Devise gem. How to remove current password when password is blank?

正如@dgmstuart的评论中所建议的那样,我删除了if !params[:current_password].blank?以避免质量分配错误。

请注意,这只是对Devise函数update_with_password的重写;原始代码在这里:

http://rubydoc.info/github/plataformatec/devise/master/Devise/Models/DatabaseAuthenticatable#update_with_password-instance_method