####devise 使用指南 参考: https://github.com/plataformatec/devise/blob/master/README.md
在Gemfile添加
gem 'devise'
rails g devise:install
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 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.
5. You can copy Devise views (for customization) to your app by running:
rails g devise:views
#####创建基于devise的User model rails generate devise User
检验devise 在layouts/applications.html.erb中添加
<% if user_signed_in? %>
<%= current_user.username %>|<%= link_to "Logout", destroy_user_session_path, method: :delete %>
<% else %>
<%= link_to "Login", new_user_session_path %>
<% end %>
#####更改默认email/pwd 注册登录模式为username/pwd
rails g migration add_username_to_users username:string
rake db:migrate
修改views/devise/sessions/new.html.erb
改:
<div><%= f.label :email %><br />
<%= f.text_field :email, :autofocus => true %></div>
为:
<div><%= f.label :username %><br />
<%= f.text_field :username, :autofocus => true %></div>
修改views/devise/registrations/new.html.erb
添加
<div><%= f.label :username %><br />
<%= f.text_field :username, :autofocus => true %></div>
######修改config/initializers/devise.rb
改为:
config.authentication_keys = [ :username ]
config.case_insensitive_keys = [ :username ]
config.strip_whitespace_keys = [ :username ]
######在controller中添加登录验证 //如下配置 表示除index 外其他action都需要登录 before_filter :authenticate_user!, except: [:index]
####为User Model添加字段 比如添加phone字段
rails g migration add_phone_to_users phone:string
在页面添加 相应html代码
并且要是该字段能被存储 修改 必须添加如下代码 在applicationController
before_filter :configure_permitted_parameters, if: :devise_controller?
protected
def configure_permitted_parameters
devise_parameter_sanitizer.for(:sign_up) << :username
devise_parameter_sanitizer.for(:account_update) << :username
end