前回ログイン機能を作った続き。
https://qiita.com/clipbord/items/0a716b797115f1368df1
細かいところを整えたり、アクセス制限かけたりしています。主にリンクメモです。
空白入力時のエラーメッセージの日本語化
Flashメッセージ
bootstrap導入
・text_fieldにclassを書く
https://qiita.com/KeyG/items/10691b0558ca0d0353b3
バリデーションエラー時のレイアウト崩れ修正
ログイン・ログアウト判定
アクセス制限
ログインユーザのみ確認可
application_controller.rb
before_action :set_current_user
def set_current_user
@current_user = User.find_by(id: session[:user_id])
end
def authenticate_user
if @current_user == nil
flash[:notice] = "ログインが必要です"
redirect_to(users_login_path)
end
end
user_controller.rb
before_action :authenticate_user, only: [:index,:show,:edit,:update,:destroy]
メーラー追加(development環境用)
参考:設定オプション
https://qiita.com/Anorlondo448/items/3af11c8a5837c2a5e0a1
参考:
https://qiita.com/hirotakasasaki/items/ec2ca5c611ed69b5e85e
基本的にこちらを参考にして、config部分のみ変更。
config/environments/development.rb
# Don't care if the mailer can't send.
config.action_mailer.raise_delivery_errors = false
config.action_mailer.perform_caching = false
#下記追記
config.action_mailer.default_url_options = { host: 'localhost:3000' }
config.action_mailer.delivery_method = :letter_opener
まずは、letter_openerでやってみた。
メーラー追加(production環境用)
参考:https://rails-ambassador.herokuapp.com/tips/HerokuSendmailAddon
上記を参考にSendGridを使ってherokuでメール送信しました。
config/environments/production.rb
config.action_mailer.perform_caching = false
config.action_mailer.delivery_method = :smtp
config.action_mailer.perform_deliveries = true
config.action_mailer.smtp_settings = {
:domain => 'herokuapp.com',
:address => 'smtp.sendgrid.net',
:port => 587,
:user_name => ENV['SENDGRID_USERNAME'],
:password => ENV['SENDGRID_PASSWORD'],
:authentication => :plain,
:enable_starttls_auto => true
}
欄外
devise使用。
色々調べてたら、deviseを使ったログイン機能の作成というものが出てきたので、
新しくプロジェクト作ってやってみた。次作る時はこちらをベースにしようと思った。