Skip to content

Commit 8fa3223

Browse files
author
Ryan Bigg
committed
Section 7.4.4: Add the ability to create users through the admin backend
1 parent d68f9e8 commit 8fa3223

File tree

9 files changed

+88
-2
lines changed

9 files changed

+88
-2
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
class Admin::BaseController < ApplicationController
22
before_action :authorize_admin!
3+
4+
def index
5+
end
36
end
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
11
class Admin::UsersController < Admin::BaseController
22
def index
3+
@users = User.order(:email)
34
end
5+
6+
def new
7+
@user = User.new
8+
end
9+
10+
def create
11+
params = user_params.dup
12+
user_params[:password_confirmation] = user_params[:password]
13+
@user = User.new(params)
14+
15+
if @user.save
16+
flash[:notice] = "User has been created."
17+
redirect_to admin_users_path
18+
else
19+
flash.now[:alert] = "User has not been created."
20+
render :action => "new"
21+
end
22+
end
23+
24+
private
25+
def user_params
26+
params.require(:user).permit(:name,
27+
:email,
28+
:password,
29+
:password_confirmation)
30+
end
431
end
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<%= link_to "Users", admin_users_path %>
2+
Welcome to Ticketee's Admin Lounge. Please enjoy your stay.
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<%= simple_form_for [:admin, @user] do |f| %>
2+
<% if @user.errors.any? %>
3+
<div id="error_explanation">
4+
<h2><%= pluralize(@user.errors.count, "error") %> prohibited
5+
this user from being saved:</h2>
6+
<ul>
7+
<% @user.errors.full_messages.each do |msg| %>
8+
<li><%= msg %></li>
9+
<% end %>
10+
</ul>
11+
</div>
12+
<% end %>
13+
14+
<%= f.input :email %>
15+
<%= f.input :password %>
16+
17+
<%= f.button :submit %>
18+
<% end %>
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
<h1>Admin::Users#index</h1>
2-
<p>Find me in app/views/admin/users/index.html.erb</p>
1+
<%= link_to "New User", new_admin_user_path, class: "new" %>
2+
<ul>
3+
<% @users.each do |user| %>
4+
<li><%= link_to user.email, [:admin, user] %></li>
5+
<% end %>
6+
</ul>
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<h2>New User</h2>
2+
3+
<%= render "form" %>

ticketee/app/views/layouts/application.html.erb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,9 @@
3232
<%= link_to "Sign in", new_user_session_path %>
3333
</li>
3434
<% end %>
35+
<% admins_only do %>
36+
<li><%= link_to "Admin", admin_root_path %></li>
37+
<% end %>
3538
</ul>
3639

3740
<% if user_signed_in? %>

ticketee/config/routes.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
Rails.application.routes.draw do
22
devise_for :users
3+
4+
namespace :admin do
5+
root "base#index"
6+
resources :users
7+
end
8+
39
root 'projects#index'
410

511
resources :projects do
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
require "rails_helper"
2+
3+
feature "Creating Users" do
4+
let!(:admin) { FactoryGirl.create(:admin_user) }
5+
6+
before do
7+
login_as(admin)
8+
visit "/"
9+
click_link "Admin"
10+
click_link "Users"
11+
click_link "New User"
12+
end
13+
14+
scenario 'Creating a new user' do
15+
fill_in "Email", with: "[email protected]"
16+
fill_in "Password", with: "password"
17+
click_button "Create User"
18+
expect(page).to have_content("User has been created.")
19+
end
20+
end

0 commit comments

Comments
 (0)