项目作者: nrogap

项目描述 :
this repo follow GoRails tutorial clip "User Authentication in Rails with Devise" https://youtu.be/ef11ToOE4N0
高级语言: Ruby
项目地址: git://github.com/nrogap/rails-devise.git
创建时间: 2018-12-25T03:24:30Z
项目社区:https://github.com/nrogap/rails-devise

开源协议:

下载


Rails and Devise

Devise is a useful authentication library. This repository follow the GoRails tutorial.

installation

  1. gem install devise
  2. bundle install

setup

after run this below command, follow the suggestion 5 steps in console

  1. rails generate devise:install

generate User tables by devise helper

  1. rails generate devise user
  2. rake db:migrate

Routes

on file routes.rb we will get this line

  1. devise_for :users

check routes by rails routes.

  • new_user_registration_path register new user
  • new_user_session_path login user
  • destroy_user_session_path log out user

Custom Devise’s Views

  • rails generate devise:views
  • go to app/views/devise/

    Helper

    View

  • <%= notice %> notice message from devise such as ‘login success’
  • <%= alert %> alert or error message from devise such as ‘need to login`
  • <% if user_signed_in? %>
  • <%= current_user %> such as <%= current_user.email %>

Controller

check user session before access

  1. class BooksController < ApplicationController
  2. before_action :authenticate_user!, except: [:index, :show]

create Book with current user

  1. # app/models/user.rb
  2. class User < ApplicationRecord
  3. ...
  4. has_many :books
  5. ...
  1. # app/models/book.rb
  2. class Book < ApplicationRecord
  3. ...
  4. belongs_to :books
  5. ...
  1. class BooksController < ApplicationController
  2. ...
  3. def create
  4. # @book = Book.new(book_params) # original
  5. @book = current_user.books.new(book_params) # can use current_user for create books
  6. ...
  7. end
  8. ...

Resource