Skip to content

Commit 5eaa953

Browse files
committed
add user crud action
1 parent d0d9e35 commit 5eaa953

File tree

10 files changed

+132
-9
lines changed

10 files changed

+132
-9
lines changed

app/controllers/sessions_controller.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ def create
1010
start_new_session_for user
1111
redirect_to after_authentication_url
1212
else
13-
redirect_to new_session_path, alert: "Try another email address or password."
13+
redirect_to connexion_path, alert: "Try another email address or password."
1414
end
1515
end
1616

1717
def destroy
1818
terminate_session
19-
redirect_to new_session_path
19+
redirect_to connexion_path
2020
end
2121
end
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,54 @@
11
class UsersController < ApplicationController
2+
before_action :set_user, only: %i[ edit update destroy ]
3+
24
def index
35
@users = User.all
46
#users = User.all
57

68
end
79

810
def create
11+
@user = User.new(user_params)
912

13+
respond_to do |format|
14+
if @user.save
15+
format.html { redirect_to users_path, notice: "User was successfully created." }
16+
else
17+
format.html { render :new, status: :unprocessable_entity }
18+
end
19+
end
1020
end
21+
22+
1123
def new
24+
@user = User.new
1225
end
1326
def edit
1427
end
28+
29+
def update
30+
respond_to do |format|
31+
if @user.update(user_params)
32+
format.html { redirect_to users_path, notice: "User was successfully updated." }
33+
else
34+
format.html { render :edit, status: :unprocessable_entity }
35+
end
36+
end
37+
end
1538
def destroy
39+
@user.destroy!
40+
41+
respond_to do |format|
42+
format.html { redirect_to users_path, status: :see_other, notice: "User was successfully destroyed." }
43+
end
44+
end
45+
46+
private
47+
def set_user
48+
@user = User.find(params.expect(:id))
1649
end
1750

51+
def user_params
52+
params.expect(user: [:email, :password, :is_admin])
53+
end
1854
end

app/views/layouts/_sidebar.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
</li>
8484

8585
<li class="sidebar-item ">
86-
<a href="application-chat.html" class='sidebar-link'>
86+
<a href="<%= users_path%>" class='sidebar-link'>
8787
<i class="bi bi-people"></i>
8888
<span>Users</span>
8989
</a>

app/views/layouts/mazer.html.erb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
</head>
2828

2929
<body>
30+
<!-- Only for connexion and reset password not display sidebar -->
3031
<% unless current_page?('/connexion') || current_page?('/reset_password') %>
3132
<%= render "layouts/sidebar" %>
3233

@@ -36,9 +37,7 @@
3637
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/static/js/initTheme.js"></script>
3738
<!-- Start content here -->
3839

39-
<%= render "layouts/sidebar" %>
40-
41-
<div id="main"><%= yield %></div>
40+
<%#= render "layouts/sidebar" %>
4241

4342
<!-- End content -->
4443
<script src="https://cdn.jsdelivr.net/gh/zuramai/mazer@docs/demo/assets/static/js/components/dark.js"></script>

app/views/users/_form.html.erb

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<%= form_with(model: user) do |form| %>
2+
<% if user.errors.any? %>
3+
<div style="color: red">
4+
<h2><%= pluralize(user.errors.count, "error") %> prohibited this user from being saved:</h2>
5+
6+
<ul>
7+
<% user.errors.each do |error| %>
8+
<li><%= error.full_message %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<div class="col-md-4">
15+
<div class="form-group">
16+
<%= form.label :email, style: "display: block" %>
17+
<%= form.text_field :email, id: "basicInput", class: "form-control", autofocus: true %>
18+
</div>
19+
</div>
20+
<div class="col-md-4">
21+
<div class="form-group">
22+
<%= form.label :password, style: "display: block" %>
23+
<%= form.text_field :password, id: "basicInput", class: "form-control", autofocus: true %>
24+
</div>
25+
</div>
26+
<div class="col-md-4">
27+
<div class="form-group">
28+
<%= form.label :is_admin, style: "display: block" %>
29+
<%= form.check_box :is_admin, autofocus: true %>
30+
</div>
31+
</div>
32+
<div class="mt-4">
33+
<div class="col-12 d-flex justify-content-end">
34+
<%= form.submit "Save User", class: "btn btn-primary me-1 mb-1" %>
35+
<%= link_to "Cancel", users_path, class: "btn btn-light-secondary me-1 mb-1" %>
36+
</div>
37+
</div>
38+
<% end %>

app/views/users/edit.html.erb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<% content_for :title, "Editing user" %>
2+
3+
<h1>Editing user</h1>
4+
5+
<%= render "form", user: @user %>
6+
7+
<div class="mt-4">
8+
<%= link_to "Show this user", @user %> |
9+
<%= link_to "Back to users", users_path %>
10+
</div>

app/views/users/index.html.erb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@
44
<section class="section">
55
<div class="card">
66
<div class="card-body">
7+
8+
<i class="bi bi-plus"></i> <%= link_to "New User?",new_user_path, class:'btn btn-primary'%> </i>
79
<table class="table table-striped" id="table1">
810
<thead>
911
<tr>
1012
<th>Email</th>
1113
<th>Administrator</th>
12-
14+
<th> Users actions </th>
1315
</tr>
1416
</thead>
1517
<tbody>
@@ -18,6 +20,14 @@
1820
<tr>
1921
<td> <%= user.email %></td>
2022
<td> <%= user.is_admin %></td>
23+
<td class="text-end">
24+
25+
<%= link_to edit_user_path(user), class: "btn btn-secondary btn-sm" do %>
26+
<i class="bi bi-pencil"></i> Edit
27+
<% end %>
28+
<%= button_to user, data: { confirm: "Are you sure?" }, method: :delete, class: "btn btn-danger btn-sm" do %>
29+
<i class="bi bi-trash"></i> Delete
30+
<% end %>
2131
</tr>
2232
<% end %>
2333
<tbody>

app/views/users/new.html.erb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<% content_for :title, "New user" %>
2+
3+
<section id="multiple-column-form">
4+
<div class="row match-height">
5+
<div class="col-12">
6+
<div class="card">
7+
<div class="card-header">
8+
<h4>Create New User</h4>
9+
</div>
10+
<div class="card-content">
11+
<div class="card-body">
12+
<p>User should ... Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed do eiusmod tempor incididunt ut
13+
labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip
14+
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla
15+
pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est
16+
laborum.</p>
17+
<%= render "form", user: @user %>
18+
</div>
19+
</div>
20+
</div>
21+
</div>
22+
</div>
23+
</section>

config/routes.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
resources :regions
33
root "home#index"
44
get "home/index", as: :home
5-
resource :session
5+
resource :session, except:[:new]
66
resources :passwords, except: [:new], param: :token
7-
resources :users, only: :create
87
resource :registration, only: %i[new create]
98
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html
109
get "connexion", to: "sessions#new"

spec/models/user_spec.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require 'rails_helper'
2+
3+
RSpec.describe User, type: :model do
4+
subject { create(:user) }
5+
it { should validate_presence_of(:email) }
6+
it { should validate_presence_of(:password) }
7+
it { should validate_uniqueness_of(:email).ignoring_case_sensitivity }
8+
end

0 commit comments

Comments
 (0)