OSは、winXPで
railsinstaller-1.2.0.exeをインストール済みが前提
1、rails new で、適当にrailsアプリを作る
2、Gemfileの最後に以下を追加
gem "devise"
3、Deviseをインストール
>bundle install
4、Deviseをアプリに組み込み
>rails generate devise:install
create config/initializers/devise.rb
create config/locales/devise.en.yml
===============================================================================
Some setup you must do manually if you haven't yet:
1. Setup default url options for your specific environment. Here is an
example of development environment:
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
This is a required Rails configuration. In production it must be 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>
===============================================================================
5、上記のコメント(サンプル)に対応する
まず、
config/routes.rb------------------------------
root :to => "home#index"
Mysample1::Application.routes.draw do
resources :books
end
config/routes.rb end---------------------------
app/views/layouts/application.html.erb -------------------
<body>
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
<%= yield %>
</body>
app/views/layouts/application.html.erb end ----------------------
6、上記のコメントに合わせ、routes.rbに記述したhome コントローラの index アクションを生成
>rails generate controller home index
7、public/index.html を削除
8、「
http://localhost:3000/」で、Home#indexが表示される
9、deviseのViewを生成する(app/viewsにdeviseフォルダを生成(数多くの ERB ファイルがその中に生成される))
>rails generate devise:views
10、Userモデルを作成
>rails generate devise user
>rake db:migrate
11、config/routes.rbに「devise_for :user」を追加
config/routes.rb------------------------------
root :to => "home#index"
devise_for :user
Mysample1::Application.routes.draw do
resources :books
end
config/routes.rb end---------------------------
12、「http:localhost:3000/user/sign_in」で確認
13、ログイン時のみログアウトのリンクを表示するには、以下を「app/views/layouts/application.html.erb 」等に書き込む
<% if user_signed_in? %>
<%= link_to "Sign out", destroy_user_session_path, :method => :delete %>
<% end %>
以上
参考:
Rails3 × Deviseでユーザー認証機能を追加する
http://d.hatena.ne.jp/t-taira/20110101/1293843821第12回: ユーザー認証(1)
http://www.oiax.jp/rails/rails3/authentication1.html13さいの備忘録 : Rails3でログイン認証(1) deviseインストール編
http://blog.livedoor.jp/nizoraul/archives/3555323.htmlDeviseでログイン時のみログアウトのリンクを表示する方法
http://d.hatena.ne.jp/kaorumori/20110811/1313647097