0

更新 1:更新的问题陈述

问题陈述

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

我意识到这有点像初学者的问题,但我是初学者。热爱 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

app/views/devise/registrations/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
4

1 回答 1

0

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

Ruby on Rails,设计宝石。密码为空时如何删除当前密码?

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

请注意,这只是对设计函数 update_with_password 的覆盖;原始代码在这里:

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

于 2013-06-22T16:48:25.150 回答