牌語備忘録 -pygo

あくまでもメモです。なるべくオフィシャルの情報を参照してください。

牌語備忘録 -pygo

Rails3でユーザ認証の『devise』を使ってみる

*1

Gemfile

gem 'devise'

bundle install

bundle install
:

rails g devise:install

最後に表示される setup を行う

rails g 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>

  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 g devise User

ユーザモデル作成

rails g devise User
      invoke  active_record
      create    db/migrate/201202xxxxxxxx_devise_create_users.rb
      create    app/models/user.rb
      invoke    test_unit
      create      test/unit/user_test.rb
      create      test/fixtures/users.yml
      insert    app/models/user.rb
       route  devise_for :users

db:migrate

rake db:migrate

Migration

201202xxxxxxxx_devise_create_users.rb

Confirmable はデフォルトでコメントアウトされてた。(verによって違う?)

class DeviseCreateUsers < ActiveRecord::Migration
  def change
    create_table(:users) do |t|
      ## Database authenticatable
      t.string :email,              :null => false, :default => ""
      t.string :encrypted_password, :null => false, :default => ""

      ## Recoverable
      t.string   :reset_password_token
      t.datetime :reset_password_sent_at

      ## Rememberable
      t.datetime :remember_created_at

      ## Trackable
      t.integer  :sign_in_count, :default => 0
      t.datetime :current_sign_in_at
      t.datetime :last_sign_in_at
      t.string   :current_sign_in_ip
      t.string   :last_sign_in_ip

      ## Encryptable
      # t.string :password_salt

      ## Confirmable
      # t.string   :confirmation_token
      # t.datetime :confirmed_at
      # t.datetime :confirmation_sent_at
      # t.string   :unconfirmed_email # Only if using reconfirmable

      ## Lockable
      # t.integer  :failed_attempts, :default => 0 # Only if lock strategy is :failed_attempts
      # t.string   :unlock_token # Only if unlock strategy is :email or :both
      # t.datetime :locked_at

      ## Token authenticatable
      # t.string :authentication_token


      t.timestamps
    end

    add_index :users, :email,                :unique => true
    add_index :users, :reset_password_token, :unique => true
    # add_index :users, :confirmation_token,   :unique => true
    # add_index :users, :unlock_token,         :unique => true
    # add_index :users, :authentication_token, :unique => true
  end
end
|<

**rake db:migrate
>|sh|
rake db:migrate

View

application.html.erb

ボタンとメアド表示
Sign up, Sign in, Sign out and user email
For example:

    <% if notice %>
      <p class="notice alert"><%= notice %></p>
    <% end %>
    <% if alert %>
      <p class="alert alert-error"><%= alert %></p>
    <% end %>

    <% if user_signed_in?%>
      <p><%= current_user.email %></p>
      <p class="btn"><%= link_to "Sign out", destroy_user_session_path, :method => :delete %></p>
    <% else %>
      <p class="btn"><%= link_to "Sign up", new_user_registration_path %></p>
      <p class="btn"><%= link_to "Sign in", new_user_session_path %></p>
    <% end %>
application_controller.rb*2

アクセス制限
For example:

  before_filter :authenticate_user!

check: /users/sign_up

rails s
ブラウザで『/users/sign_up』を確認

*1:MacOSX10.7, ruby19.2, rails3.1.3, devise2.0.0

*2:修正:20120203