読者です 読者をやめる 読者になる 読者になる

私の歴史と今

読んでて恥ずかしくなるのが私の歴史。だけどそのときは今現在のように真面目に書いていた訳でね。そんな私の今を書いていく。

rails4でdeviseを使う

基本的にはdeviseのGetting startedに従う → https://github.com/plataformatec/devise

設定ファイル類の作成

bash-3.2$ bundle exec 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. 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 on Heroku with Rails 3.2 only, 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.

  5. You can copy Devise views (for customization) to your app by running:

       rails g devise:views

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

#1~5の手順に従う。

development.rbに以下を追加。

config.action_mailer.default_url_options = { :host => 'localhost:3000' }
bash-3.2$ bundle exec rails g devise:views         
      invoke  Devise::Generators::SharedViewsGenerator
      create    app/views/devise/shared
      create    app/views/devise/shared/_links.erb
      invoke  form_for
      create    app/views/devise/confirmations
      create    app/views/devise/confirmations/new.html.erb
      create    app/views/devise/passwords
      create    app/views/devise/passwords/edit.html.erb
      create    app/views/devise/passwords/new.html.erb
      create    app/views/devise/registrations
      create    app/views/devise/registrations/edit.html.erb
      create    app/views/devise/registrations/new.html.erb
      create    app/views/devise/sessions
      create    app/views/devise/sessions/new.html.erb
      create    app/views/devise/unlocks
      create    app/views/devise/unlocks/new.html.erb
      invoke  erb
      create    app/views/devise/mailer
      create    app/views/devise/mailer/confirmation_instructions.html.erb
      create    app/views/devise/mailer/reset_password_instructions.html.erb
      create    app/views/devise/mailer/unlock_instructions.html.erb

Modelクラス作成

bash-3.2$ bundle exec rails g devise User
      invoke  active_record
      create    db/migrate/20130826142525_devise_create_users.rb
      create    app/models/user.rb
      insert    app/models/user.rb
       route  devise_for :users

db:migrate

bash-3.2$ bundle exec rake db:migrate
==  DeviseCreateUsers: migrating ==============================================
-- create_table(:users)
   -> 0.0118s
-- add_index(:users, :email, {:unique=>true})
   -> 0.0371s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.0234s
==  DeviseCreateUsers: migrated (0.0725s) =====================================

認証するように変更。ApplicationControllerに以下を追加。

before_filter :authenticate_user!

コントローラ作成

bash-3.2$ bundle exec rails g controller devise::sessions_controller
      create  app/controllers/devise/sessions_controller_controller.rb
      invoke  erb
      create    app/views/devise/sessions_controller
      invoke  helper
      create    app/helpers/devise/sessions_controller_helper.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/devise/sessions_controller.js.coffee
      invoke    scss
      create      app/assets/stylesheets/devise/sessions_controller.css.scss
bash-3.2$ bundle exec rails g controller devise::registrations_controller
      create  app/controllers/devise/registrations_controller_controller.rb
      invoke  erb
      create    app/views/devise/registrations_controller
      invoke  helper
      create    app/helpers/devise/registrations_controller_helper.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/devise/registrations_controller.js.coffee
      invoke    scss
      create      app/assets/stylesheets/devise/registrations_controller.css.scss
bash-3.2$ bundle exec rails g controller devise::passwords_controller    
      create  app/controllers/devise/passwords_controller_controller.rb
      invoke  erb
      create    app/views/devise/passwords_controller
      invoke  helper
      create    app/helpers/devise/passwords_controller_helper.rb
      invoke  assets
      invoke    coffee
      create      app/assets/javascripts/devise/passwords_controller.js.coffee
      invoke    scss
      create      app/assets/stylesheets/devise/passwords_controller.css.scss

routesを確かめる。

bash-3.2$ bundle exec rake routes
                  Prefix Verb   URI Pattern                       Controller#Action
        new_user_session GET    /users/sign_in(.:format)          devise/sessions#new
            user_session POST   /users/sign_in(.:format)          devise/sessions#create
    destroy_user_session DELETE /users/sign_out(.:format)         devise/sessions#destroy
           user_password POST   /users/password(.:format)         devise/passwords#create
       new_user_password GET    /users/password/new(.:format)     devise/passwords#new
      edit_user_password GET    /users/password/edit(.:format)    devise/passwords#edit
                         PATCH  /users/password(.:format)         devise/passwords#update
                         PUT    /users/password(.:format)         devise/passwords#update
cancel_user_registration GET    /users/cancel(.:format)           devise/registrations#cancel
       user_registration POST   /users(.:format)                  devise/registrations#create
   new_user_registration GET    /users/sign_up(.:format)          devise/registrations#new
  edit_user_registration GET    /users/edit(.:format)             devise/registrations#edit
                         PATCH  /users(.:format)                  devise/registrations#update
                         PUT    /users(.:format)                  devise/registrations#update
                         DELETE /users(.:format)                  devise/registrations#destroy

I18n対応。以下のURLの日本語版をコピってデフォルトとする。ファイルはdevise.ja.yml

https://github.com/plataformatec/devise/wiki/I18n

viewの日本語化。

デフォルトでは以下のように英語仕様になっている。(devise/registrations/new.html.erbの例)

<h2>Sign in</h2>

<%= form_for(resource, :as => resource_name, :url => registration_path(resource_name)) do |f| %>
  <%= devise_error_messages! %>

twitter bootstrap用に書き換える

以下のようになっているので、

<h2>ログイン</h2>

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
  <div><%= f.label :email %><br />
  <%= f.email_field :email, :autofocus => true %></div>

  <div><%= f.label :password %><br />
  <%= f.password_field :password %></div>

  <% if devise_mapping.rememberable? -%>
    <div><%= f.check_box :remember_me %> <%= f.label :remember_me %></div>
  <% end -%>

  <div><%= f.submit "ログイン" %></div>
<% end %>

<%= render "devise/shared/links" %>

例えば以下のように書き直す。

<h2>ログイン</h2>

<%= form_for(resource, :as => resource_name, :url => session_path(resource_name), :html=>{:class=>"col-lg-6"}) do |f| %>
  <div class="field form-group"><%= f.label :email, :class=>"control-label" %>
  <%= f.email_field :email, :autofocus => true, :class=>"form-control" %></div>

  <div class="field form-group"><%= f.label :password, :class=>"control-label"%>
  <%= f.password_field :password, :class=>"form-control" %></div>

  <% if devise_mapping.rememberable? -%>
    <div class="field form-group">
      <%= f.label :remember_me %>
      <%= f.check_box :remember_me, :class=>"form-control" %></div>
  <% end -%>

  <div class="actions form-group"><%= f.submit "ログイン", :class=>"btn btn-primary btn-lg" %></div>

<%= render "devise/shared/links" %>

<% end %>