16
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

deviseに関して

Posted at

よく忘れるのでメモ

導入

まずはじめは

gem 'devise'

とGemfileに書く。次に

rails generate devise:install

とするのだけど、なにも設定してないと下記のようなエラー


chabashilah@CHABA_MAC% rails generate devise:install                                                               [~/prv/.../heroku/senstat]
      create  config/initializers/devise.rb
      create  config/locales/devise.en.yml
===============================================================================

Some setup you must do manually if you haven't yet:

  1. Ensure you have defined default url options in your environments files. Here 
     is an example of default_url_options appropriate for a development environment 
     in config/environments/development.rb:

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

     In production, :host should be set to the actual host of your application.

  2. Ensure you have defined root_url to *something* in your config/routes.rb.
     For example:

       root :to => "home#index"

  3. Ensure you have flash messages in app/views/layouts/application.html.erb.
     For example:

       <p class="notice"><%= notice %></p>
       <p class="alert"><%= alert %></p>

  4. If you are deploying Rails 3.1 on Heroku, you may want to set:

       config.assets.initialize_on_precompile = false

     On config/application.rb forcing your application to not access the DB
     or load models when precompiling your assets.

===============================================================================

とても親切。仰るとおりにする。それを終えたら、もう一度

rails generate devise:install

を打つ。すると

    conflict  config/initializers/devise.rb
Overwrite /Users/chabashilah/Dropbox/chabashilah/prv/dev/git/heroku/senstat/config/initializers/devise.rb? (enter "h" for help)

とでるが、Yを押して先に進む。
するとまた同じエラーが出る。。。なんでだろう。。。
あ、ちがう。これエラーじゃないんだ!と気づく。
気を取りなおして次に進む。
Modelを作成するわけですが、まぁUserと名をつけておけばいいでしょう。ということで

rails generate devise User

migrateするのをわすれずに

rake db:migrate

さて次に、Controllerを作成します。

rails generate controller home index

とりあえずのログイン情報を表示するためviews/home/index.html.erbに

<h1>TOP</h1>
	<% if user_signed_in? %>
	<%= link_to "Logout", 	destroy_user_session_path, method: delete %>
	<% else %>
	<%= link_to "Login", new_user_session_path %>
<% end %>

を追加する。

ログイン画面のデザインなどを変更したいので、ここでViewを作成する。

rails generate devise:views

さて使いましょう

ログインしてないUserがページを見れないようにするためには
Controllerでbefore_filter :authenticate_user!
加える。

class SensorController < ApplicationController
  before_filter :authenticate_user!

とりあえずここまで。時間があるときに追記する予定。

#Reference

https://github.com/plataformatec/devise
http://kitbc.s41.xrea.com/main/?use_devise

16
17
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
16
17

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?