rails组件_devise_用户系统
2017-04-24 本文已影响154人
jmukirin
ruby环境
➜ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin15]
➜ rails -v
Rails 5.0.2
先建立一个rails项目
rails new blog
在Gemfile里添加
gem 'devise'
然后运行
bundle install
安装好了devise的gem包以后, 安装devise相关组件
➜ blog/ rails generate devise:install
Running via Spring preloader in process 38288
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', port: 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. You can copy Devise views (for customization) to your app by running:
rails g devise:views
/config/environments/development.rb 添加
# devise setting
config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
/config/routes.rb 添加
root to: "home#index"
/app/views/layout/application.html.erb 添加
<p class="notice"><%= notice %></p>
<p class="alert"><%= alert %></p>
输入命令rails g devise:views 会生成devise的视图文件
➜ blog rails g devise:views
Running via Spring preloader in process 38720
Expected boolean default value for '--markerb'; got :erb (string)
invoke Devise::Generators::SharedViewsGenerator
create app/views/devise/shared
create app/views/devise/shared/_links.html.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/password_change.html.erb
create app/views/devise/mailer/reset_password_instructions.html.erb
create app/views/devise/mailer/unlock_instructions.html.erb
输入命令rails g devise user 生成需要用到devise的模型, 路由中会自动生成一个devise_for :users
➜ blog rails g devise user
Running via Spring preloader in process 38828
invoke active_record
create db/migrate/20170424093829_devise_create_users.rb
create app/models/user.rb
invoke test_unit
create test/models/user_test.rb
create test/fixtures/users.yml
insert app/models/user.rb
route devise_for :users
输入命令rake db:migrate 执行迁移文件
➜ blog rake db:migrate
== 20170424093829 DeviseCreateUsers: migrating ================================
-- create_table(:users)
-> 0.0014s
-- add_index(:users, :email, {:unique=>true})
-> 0.0007s
-- add_index(:users, :reset_password_token, {:unique=>true})
-> 0.0006s
== 20170424093829 DeviseCreateUsers: migrated (0.0029s) =======================
输入命令rails generate controller home index 用来设置root页面
➜ blog rails generate controller home index
Running via Spring preloader in process 39045
create app/controllers/home_controller.rb
route get 'home/index'
invoke erb
create app/views/home
create app/views/home/index.html.erb
invoke test_unit
create test/controllers/home_controller_test.rb
invoke helper
create app/helpers/home_helper.rb
invoke test_unit
invoke assets
invoke coffee
create app/assets/javascripts/home.coffee
invoke scss
create app/assets/stylesheets/home.scss
在控制器中, 设置访问之前需要登录
class HomeController < ApplicationController
before_action :authenticate_user!, :only => [:index, :new]
def index
end
end
解释一下:
user_signed_in? //判断用户是否登录
current_user //获取当前登录用户
user_session //可以访问对应的session
现在可以跑一下rails s 来看看效果
访问默认页 http://127.0.0.1:3000/ 会被路由到 http://127.0.0.1:3000/users/sign_in
你将看到以下简陋的页面, 实现了最简单的用户系统.
注册页(users/sign_up
),
登入页(users/sign_in
),
忘记密码页(users/password/new
)
登录之后, devise默认跳转到了root, 要自定义, 去覆盖after_sign_in_path_for
和after_sign_out_path_for
来自定义跳转回调
待续写