1

我需要向 Rails 应用程序中的模型添加确认电子邮件功能,但仅此而已。它不是一个用户模型,它是不可验证的。

我添加devise :confirmable到模型中,并运行迁移:

class AddConfirmableToProjects < ActiveRecord::Migration
  def up
    add_column :projects, :confirmation_token, :string
    add_column :projects, :confirmed_at, :datetime
    add_column :projects, :confirmation_sent_at, :datetime
    add_index :projects, :confirmation_token, :unique => true
  end

  def down
    remove_column :projects, :confirmation_token, :confirmed_at, :confirmation_sent_at
  end
end

但是当我创建一个新项目时,我得到:Could not find a valid mapping for #<Project...

4

2 回答 2

1

将 :confirmable 添加到不是您的用户模型的模型听起来有点奇怪。你确定吗?

# Confirmable is responsible to verify if an account is already confirmed to
# sign in, and to send emails with confirmation instructions.

如果,这是运行规范/测试后返回的错误吗?如果您使用 RSpec 运行 FactoryGirl,请尝试添加config.cache_classes = truetest.rb 文件。这有点阴暗,但看起来是唯一的解决方案。

如果没有,请提供更多代码(模型、控制器、视图)。

于 2013-04-21T00:52:59.340 回答
0

是的,我们可以为任何型号设置可确认的。以下是执行此操作的步骤。假设我有模型 a Invitation

  1. 添加devise :confirmableInvitation
  2. 这个模型应该有属性:email
  3. 使用以下列创建迁移:

    t.string   "email"
    t.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    
  4. 创建一个需要扩展的控制器Devise::ConfirmationsController。在该控制器中添加以下代码:

    def create
      self.resource = resource_class.send_confirmation_instructions(params[resource_name])
      if successful_and_sane?(resource)
        respond_with({}, :location => root_url)
      else
        # code your logic
      end
    end
    
    def new; end
    
    def show; end
    
    • confirmation_instruction.html.erb在“app/views/devise/mailer/”下创建一个电子邮件视图

    • 以下行将在您的电子邮件中创建确认 URL:<%= confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %>

    • 现在创建您的模型“邀请”的新记录Invitation.create(:email => params[:email]

    • 现在成功创建后,记录将保存在数据库中,电子邮件也将发送到该电子邮件。

于 2013-05-23T13:05:27.337 回答