LoginSignup
27
29

More than 5 years have passed since last update.

HerokuでDeviseを使用する手順 on Rails3.2.13, ruby1.9.3, Sendgrid

Last updated at Posted at 2013-09-24

=======

deviseのインストール

gem 'devise'
$ bundle install
$ rails g devise:install
config/environments/development.rb
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
config/environments/production.rb
config.action_mailer.default_url_options = { :host => 'appname.herokuapp.com' }
confin/routes.rb
root :to => "top#index"

ここでは仮にtop#indexをルートパスに指定。

app/views/layouts/application.html.erb
<%= notice %>
<%= alert %>

メッセージを表示したければ↑

Herokuの場合、以下の設定が必要

config/application.rb
config.assets.initialize_on_precompile = false

deviseでUserモデルを作成

$ rails g devise User

とすると、user.rbモデルとマイグレーションファイルが生成されますが、まだmigrateは行わないで下さい。

app/models/user.rb
devise :database_authenticate, :registerable,
           :recoverable, :rememberable, :trackable, :validatable, :confirmable # confirmableを追加
db/migrate/2013xxxxxxx_devise_create_users.rb
## Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable

viewの生成

$ rails g devise:views
app/views/layouts/application.html.erbの適切な場所に追記。
<% if user_signed_in? %>
  <li><%= link_to "Logout", destroy_user_session_path, method: :delete %></li>
<% else %>
  <li><%= link_to "Login", new_user_session_path %></li>
<% end %>
ログインの制限をかけたいControllerに記述.
before_filter :authenticate_user!

development環境で確認

$ rake db:migrate
$ rails s

サインアップすると、標準出力に

Confirm my account

とURL(localhost)が出力されているので、そのURLにアクセスして、登録したアカウントを有効化する。

Herokuで確認(メールを送信するまで確認)

導入方法は2つあります

  1. Sendgridのプラグインを導入する
  2. gmailのsmtp認証を使う

今回は1のSendgridで進めます。
以下のコマンドは、herokuのアカウントでSendGridのアドオンの利用登録(クレジットカードの登録)を済ませておかないと叩けません。無料枠でも利用するには登録が必要みたいです。

$ heroku addons:add sendgrid:starter

すると自動的にHerokuでSendGridのアカウントが取得されます。
アカウント名とパスを表示するには、

$ heroku config:get SENDGRID_USERNAME
$ heroku config:get SENDGRID_PASSWORD

を実行します。
SMTP認証が有効になるまで2時間ほどかかる場合もあるかもしれません。
ちなみに、こちらのユーザ名、パスワードでSendgridのWEBページにログインできます。

config/environment.rb
ActionMailer::Base.delivery_method = :smtp
ActionMailer::Base.smtp_settings = {
  :user_name => "yourSendGridname you get from heroku",
  :password => "yourSendGridname you get from heroku",
  :domain => "heroku.com",
  :address => "smtp.sendgrid.net",
  :port => 587,
  :authentication => :plain,
  :enable_starttls_auto => true
}
config/initializers/devise.rb
config.mailer_sender = 'noreply@yourdomain'

最後にデプロイして正常にDeviseが導入できたか確認する

$ git push heroku master
$ heroku run rake db:create
$ heroku run rake db:migrate

おまけ

  • 必ず.gitignoreに以下のフォルダを無視するように設定すること。
/public/assets/*

これでサインアップ時に、登録したメールアドレスにアカウントを有効にするURLが記載されたメールが届きます。
アクセスしてログインできれば設定完了です!

カスタマイズ

27
29
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
27
29