Где воздух гор - там тишина снегов, молчание камней и дремлет сила

Программирование Web

Rails и Device: аутентификации пользователей

2018-03-09 07:00:20








Пишем  строку gem 'devise' в  файл Gemfileв готовом Rails приложении запускаем скрипт установки bundle install:

C:\myapp>bundle install
Resolving dependencies...
Using rake (10.0.4)
Using i18n (0.6.1)
Using multi_json (1.7.2)
Using activesupport (3.2.13)
Using builder (3.0.4)
Using activemodel (3.2.13)
Using erubis (2.7.0)
Using journey (1.0.4)
Using rack (1.4.5)
Using rack-cache (1.2)
Using rack-test (0.6.2)
Using hike (1.2.2)
Using tilt (1.3.7)
Using sprockets (2.2.2)
Using actionpack (3.2.13)
Using mime-types (1.22)
Using polyglot (0.3.3)
Using treetop (1.4.12)
Using mail (2.5.3)
Using actionmailer (3.2.13)
Using arel (3.0.2)
Using tzinfo (0.3.37)
Using activerecord (3.2.13)
Using activeresource (3.2.13)
Using bcrypt-ruby (3.0.1)
Using bundler (1.3.5)
Using coffee-script-source (1.6.2)
Using execjs (1.4.0)
Using coffee-script (2.2.0)
Using rack-ssl (1.3.3)
Using json (1.7.7)
Using rdoc (3.12.2)
Using thor (0.18.1)
Using railties (3.2.13)
Using coffee-rails (3.2.2)
Using orm_adapter (0.4.0)
Using warden (1.2.1)
Using devise (2.2.3)
Using jquery-rails (2.2.1)
Using mysql2 (0.3.11)
Using rails (3.2.13)
Using sass (3.2.7)
Using sass-rails (3.2.6)
Using uglifier (2.0.1)
Your bundle is complete!
Use `bundle show [gemname]` to see where a bundled gem is installed.
 
C:\myapp>
 
2. Затем запускаем  скрипт установки:
C:\ myapp >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. Her
e
     is an example of default_url_options appropriate for a development environm
ent
     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.
 
===============================================================================
 
C:\ myapp>
 
Далее производим настройки в приложении, как это рекомендуется:
 
Помещаем  вфайл  «/config/environments/development.rb:» эту строку
config.action_mailer.default_url_options = { :host => 'localhost:3000' }
для установки mailerа по умолчанию для отправки сообщений пользователю.
Вфайлероутера «/config/routes.rb»
Myapp::Application.routes.draw do
  resources :users
  root :to => "users#index"
 
3. Генерируем модель пользователей для Devise
 
C:\ myapp>rails generate devise User
      invoke  active_record
      create    db/migrate/20130328195640_add_devise_to_users.rb
      insert    app/models/user.rb
       route  devise_for :users
 
Запускаемскриптмиграции:
C:\ myapp>rake db:migrate
==  AddDeviseToUsers: migrating ===============================================
-- change_table(:users)
   -> 2.1406s
-- add_index(:users, :email, {:unique=>true})
   -> 0.2344s
-- add_index(:users, :reset_password_token, {:unique=>true})
   -> 0.2031s
==  AddDeviseToUsers: migrated (2.5781s) ======================================
 
4.  Создадим views для devise:
 
C:\ myapp>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
 
C:\myapp>
В файл роутера добавим «/config/routes.rb»:
  devise_for :users
 
И наконец, если мы зайдем по этому адресу - localhost:3000/users/sign_up, здесь производится регистрация пользователей. Приложение готово к работе:
Больше можно узнать здесь:
 Devise Github project page: https://github.com/plataformatec/devise

Здесь нет комментариев


Новый комментарий:
























Яндекс.Метрика