user validation
This commit is contained in:
parent
39a89ad564
commit
ed6a5d69f6
7 changed files with 38 additions and 8 deletions
|
@ -1,10 +1,19 @@
|
|||
class ApplicationController < ActionController::Base
|
||||
helper_method :current_user
|
||||
helper_method :logged_in?
|
||||
|
||||
def current_user
|
||||
User.find_by(id: session[:user_id])
|
||||
end
|
||||
|
||||
def logged_in?
|
||||
!current_user.nil?
|
||||
end
|
||||
|
||||
def notices_from_errors(record)
|
||||
messages = record.errors.messages.map do |attribute, messages|
|
||||
messages.map { |message| "#{attribute} #{message}".capitalize }
|
||||
end
|
||||
messages.flatten
|
||||
end
|
||||
end
|
||||
|
|
|
@ -5,9 +5,13 @@ class UsersController < ApplicationController
|
|||
|
||||
def create
|
||||
@user = User.create(params.require(:user).permit(:email, :password))
|
||||
if @user.invalid?
|
||||
redirect_to '/welcome', notice: notices_from_errors(@user)
|
||||
else
|
||||
session[:user_id] = @user.id
|
||||
redirect_to '/welcome', notice: 'Account has been created'
|
||||
end
|
||||
end
|
||||
|
||||
def password_recovery_request
|
||||
@user = User.where(email: params['email']).first
|
||||
|
|
|
@ -1,4 +1,9 @@
|
|||
class User < ApplicationRecord
|
||||
has_secure_password
|
||||
has_secure_password :recovery_password, validations: false
|
||||
enum role: [:customer, :admin], _default: :customer
|
||||
|
||||
validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
|
||||
validates :role, presence: true
|
||||
validates :password, presence: true, length: { minimum: 8 }
|
||||
end
|
||||
|
|
|
@ -12,9 +12,15 @@
|
|||
|
||||
<body>
|
||||
<%= link_to 'Home', '/welcome', method: :get%>
|
||||
<% flash.each do |type, msg| %>
|
||||
<% flash.each do |type, notice| %>
|
||||
<div class='card-panel teal lighten-5'>
|
||||
<%= msg %>
|
||||
<% if notice.is_a? String %>
|
||||
<%= notice %>
|
||||
<% else %>
|
||||
<% notice.each do |msg| %>
|
||||
<div><%= msg %></div>
|
||||
<% end %>
|
||||
<% end %>
|
||||
</div>
|
||||
<% end %>
|
||||
<%= yield %>
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
<h4>Welcome</h4>
|
||||
<% if logged_in? %>
|
||||
You are Logged In, <%= current_user.email %>
|
||||
<%= button_to "Logout", '/logout', method: :get%>
|
||||
<%= button_to "Logout", '/logout', method: :get, class: 'btn' %>
|
||||
<% else %>
|
||||
<%= button_to "Login", '/login', method: :get, class: 'btn' %>
|
||||
<%= button_to "Sign Up", '/users/new', method: :get, class: 'btn' %>
|
||||
|
|
5
db/migrate/20210321135711_add_role_to_users.rb
Normal file
5
db/migrate/20210321135711_add_role_to_users.rb
Normal file
|
@ -0,0 +1,5 @@
|
|||
class AddRoleToUsers < ActiveRecord::Migration[6.1]
|
||||
def change
|
||||
add_column :users, :role, :integer
|
||||
end
|
||||
end
|
3
db/schema.rb
generated
3
db/schema.rb
generated
|
@ -10,7 +10,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 2021_03_21_093857) do
|
||||
ActiveRecord::Schema.define(version: 2021_03_21_135711) do
|
||||
|
||||
create_table "authors", force: :cascade do |t|
|
||||
t.string "first_name"
|
||||
|
@ -44,6 +44,7 @@ ActiveRecord::Schema.define(version: 2021_03_21_093857) do
|
|||
t.datetime "created_at", precision: 6, null: false
|
||||
t.datetime "updated_at", precision: 6, null: false
|
||||
t.string "recovery_password_digest"
|
||||
t.integer "role"
|
||||
end
|
||||
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue