rails中使用devise

访问量: 1134

refer to:  https://github.com/plataformatec/devise

# Gemfile
gem 'devise'

# 运行下面三个命令

$ bundle exec rails generate devise:install
$ bundle exec rails generate devise user
$ bundle exec rails g devise:views

# application controller 增加:: 
class ApplicationController < ActionController::Base
  before_action :authenticate_user!
end

# 为application.html.erb 增加 登录 ,退出等按钮
          <% if user_signed_in? %>        
          <ul class="nav navbar-nav" style='float:right'>
            <li><%= link_to "欢迎你:#{current_user.try :email}" %></li>
            <li>
            <%= link_to '退出', destroy_user_session_path, :method => :delete  %>  
            </li>
          </ul>
          <% else %>
          <ul class="nav navbar-nav" style='float:right'>
            <li>
            <%= link_to '登陆' , '/users/sign_in', :method => :get%>
            </li>
          </ul>
          <% end %>

几个核心方法:

user_signed_in? 判断用户是否登录

current_user 获取当前用户

订阅/RSS Feed

Subscribe

分类/category