1

I need to add a confirmation email functionality to a model in a Rails Application, but nothing else. It is not a User model, it is not authenticable.

I added devise :confirmable to the Model, and ran the migration:

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

But when I create a new Project I get: Could not find a valid mapping for #<Project...

timss
  • 9,982
  • 4
  • 34
  • 56
Nicolas
  • 2,297
  • 3
  • 28
  • 40

2 Answers2

1

It sounds a bit wierd to add :confirmable to a model that's not your User model. Are you sure about this?

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

If yes, is this an error returning after running your Spec/Tests? If you're running FactoryGirl with RSpec try adding config.cache_classes = true in the test.rb file. This is a bit shady, but looks like the only solution.

If no, please provide some more code (model, controller, view).

trymv
  • 175
  • 1
  • 14
  • I want to do this to verify users' email addresses: people are able to upload something to the app but they need to confirm their email after doing so. One time only. It is failing under normal circumstances, no rspec. – Nicolas Apr 21 '13 at 02:28
0

Yes, we can setup confirmable to any model. Following are the steps to do this. Lets say I have model a Invitation:

  1. Add devise :confirmable to Invitation
  2. This model should have the attribute :email
  3. Create a migration with the following columns:

    t.string   "email"
    t.string   "confirmation_token"
    t.datetime "confirmed_at"
    t.datetime "confirmation_sent_at"
    
  4. Create a controller which needs to extend Devise::ConfirmationsController. Add the following code in that controller:

    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
    
    • Create an email view confirmation_instruction.html.erb under "app/views/devise/mailer/"

    • Following line will create confirmation URL in your email: <%= confirmation_url(@resource, :confirmation_token => @resource.confirmation_token) %>

    • Now create new record of your model "Invitation" by Invitation.create(:email => params[:email]

    • Now on successful create, record will be saved in DB and email will send to that email as well.

AJcodez
  • 31,780
  • 20
  • 84
  • 118
tahniyat
  • 75
  • 1
  • 6