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
|
class ApplicationController < ActionController::Base
|
||||||
helper_method :current_user
|
helper_method :current_user
|
||||||
helper_method :logged_in?
|
helper_method :logged_in?
|
||||||
|
|
||||||
def current_user
|
def current_user
|
||||||
User.find_by(id: session[:user_id])
|
User.find_by(id: session[:user_id])
|
||||||
end
|
end
|
||||||
|
|
||||||
def logged_in?
|
def logged_in?
|
||||||
!current_user.nil?
|
!current_user.nil?
|
||||||
end
|
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
|
end
|
||||||
|
|
|
@ -5,8 +5,12 @@ class UsersController < ApplicationController
|
||||||
|
|
||||||
def create
|
def create
|
||||||
@user = User.create(params.require(:user).permit(:email, :password))
|
@user = User.create(params.require(:user).permit(:email, :password))
|
||||||
session[:user_id] = @user.id
|
if @user.invalid?
|
||||||
redirect_to '/welcome', notice: 'Account has been created'
|
redirect_to '/welcome', notice: notices_from_errors(@user)
|
||||||
|
else
|
||||||
|
session[:user_id] = @user.id
|
||||||
|
redirect_to '/welcome', notice: 'Account has been created'
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def password_recovery_request
|
def password_recovery_request
|
||||||
|
|
|
@ -1,4 +1,9 @@
|
||||||
class User < ApplicationRecord
|
class User < ApplicationRecord
|
||||||
has_secure_password
|
has_secure_password
|
||||||
has_secure_password :recovery_password, validations: false
|
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
|
end
|
||||||
|
|
|
@ -12,9 +12,15 @@
|
||||||
|
|
||||||
<body>
|
<body>
|
||||||
<%= link_to 'Home', '/welcome', method: :get%>
|
<%= link_to 'Home', '/welcome', method: :get%>
|
||||||
<% flash.each do |type, msg| %>
|
<% flash.each do |type, notice| %>
|
||||||
<div class='card-panel teal lighten-5'>
|
<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>
|
</div>
|
||||||
<% end %>
|
<% end %>
|
||||||
<%= yield %>
|
<%= yield %>
|
||||||
|
|
|
@ -1,8 +1,8 @@
|
||||||
<h4>Welcome</h4>
|
<h4>Welcome</h4>
|
||||||
<% if logged_in? %>
|
<% if logged_in? %>
|
||||||
You are Logged In, <%= current_user.email %>
|
You are Logged In, <%= current_user.email %>
|
||||||
<%= button_to "Logout", '/logout', method: :get%>
|
<%= button_to "Logout", '/logout', method: :get, class: 'btn' %>
|
||||||
<% else %>
|
<% else %>
|
||||||
<%= button_to "Login", '/login', method: :get, class: 'btn'%>
|
<%= button_to "Login", '/login', method: :get, class: 'btn' %>
|
||||||
<%= button_to "Sign Up", '/users/new', method: :get, class: 'btn'%>
|
<%= button_to "Sign Up", '/users/new', method: :get, class: 'btn' %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
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.
|
# 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|
|
create_table "authors", force: :cascade do |t|
|
||||||
t.string "first_name"
|
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 "created_at", precision: 6, null: false
|
||||||
t.datetime "updated_at", precision: 6, null: false
|
t.datetime "updated_at", precision: 6, null: false
|
||||||
t.string "recovery_password_digest"
|
t.string "recovery_password_digest"
|
||||||
|
t.integer "role"
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue