Skip to content

Latest commit

 

History

History
100 lines (67 loc) · 1.38 KB

002.md

File metadata and controls

100 lines (67 loc) · 1.38 KB

Authentication

User Route

config/routes.rb:

Rails.application.routes.draw do
  namespace :user do
    get    :sign_in,  to: 'session#new'
    post   :sign_in,  to: 'session#create'
    delete :sign_out, to: 'session#destroy'
  end

  root 'user/session#show'
end

Session Controller

$ rails generate controller user/session show new

Secure Password

Gemfile:

gem 'bcrypt'

Scaffold

$ rails generate scaffold user state name description email username password_digest roles:text
$ rake db:migrate

User Theme

$ rails generate bootswatch:themed Users --force

User Model

app/models/user.rb:

class User < ActiveRecord::Base
  has_secure_password
  serialize :roles, Array

  class << self
    def sign_in(params)
      find_by(username: params[:username]).try(:authenticate, params[:password])
    end
  end
end

Seed

db/seeds.rb:

User.create_with(name: :ruby, password: :pass).find_or_create_by(username: :user)
$ rake db:seed

Application Controller

app/controllers/application_controller.rb:

Session Controller

app/controllers/user/session_controller.rb:

Session New

app/views/user/session/new.html.erb:

Session Show

app/views/user/session/show.html.erb:

Icons

gem 'font-awesome-rails'
gem 'gravatar_image_tag'

Navbar

app/views/layouts/_navbar.html.erb: