this repo follow GoRails tutorial clip "User Authentication in Rails with Devise" https://youtu.be/ef11ToOE4N0
Devise is a useful authentication library. This repository follow the GoRails tutorial.
gem install devise
bundle install
after run this below command, follow the suggestion 5 steps in console
rails generate devise:install
generate User tables by devise helper
rails generate devise user
rake db:migrate
on file routes.rb we will get this line
devise_for :users
check routes by rails routes
.
new_user_registration_path register
new usernew_user_session_path
login userdestroy_user_session_path
log out userrails generate devise:views
app/views/devise/
<%= 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 %>
class BooksController < ApplicationController
before_action :authenticate_user!, except: [:index, :show]
# app/models/user.rb
class User < ApplicationRecord
...
has_many :books
...
# app/models/book.rb
class Book < ApplicationRecord
...
belongs_to :books
...
class BooksController < ApplicationController
...
def create
# @book = Book.new(book_params) # original
@book = current_user.books.new(book_params) # can use current_user for create books
...
end
...