Reputation: 2367
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...
Upvotes: 1
Views: 1103
Reputation: 75
Yes, we can setup confirmable to any model. Following are the steps to do this. Lets say I have model a Invitation
:
devise :confirmable
to Invitation
:email
Create a migration with the following columns:
t.string "email"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
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.
Upvotes: 0
Reputation: 185
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).
Upvotes: 1