Posts

Rake task to create a new devise user

Montag, 04. April 2011, 01:07 Uhr | roberto@vasquez-angel.de |

namespace :devise do
  desc "Create admin"
  task :create_admin, :email, :password, :needs => :environment do |t, args|
    if a = Admin.create(:email => args.email, :password => args.password)
      puts "Created admin"
    else 
      puts a.errors
    end    
  end
end  

Usage:

$> rake devise:create_admin["user@example.com","password"]

Add a new role to your site with devise

Montag, 04. April 2011, 01:06 Uhr | roberto@vasquez-angel.de |

Adding a new role (i.e. “Admin”) is easy:

$> rails generate devise Admin

Don’t forget to migrate:

$> rake db:migrate

Then you can check for the admin in your controllers:

class BackendController < ApplicationController
  before_filter :authenticate_admin!
end 

Installing Devise

Montag, 04. April 2011, 01:06 Uhr | roberto@vasquez-angel.de |

  • Add the gem to your Gemfile:
gem 'devise'
  • Install your Bundle:
$> bundle install
  • Run the generator:
$> rails generate devise:install

This will generate a initialiter in your config/initializers folder. You should check the options in config/initializers/devise.rb

  • Add default URL options to your mailer configuration in the environments files:

environments/development.rb:

config.action_mailer.default_url_options = { :host => 'localhost:3000' }

environments/production.rb:

config.action_mailer.default_url_options = { :host => 'blog.robotex.de' }