Skip to content

Commit 7a605e7

Browse files
author
Ryan Bigg
committed
Section 7.4.4: Add the ability to create users through the admin backend
1 parent bfe472b commit 7a605e7

File tree

9 files changed

+71
-2
lines changed

9 files changed

+71
-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+
params[:password_confirmation] = 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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<%= simple_form_for [:admin, @user] do |f| %>
2+
<%= f.input :email %>
3+
<%= f.input :password %>
4+
5+
<%= f.button :submit %>
6+
<% 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
@@ -37,6 +37,9 @@
3737
<% if user_signed_in? %>
3838
<div class="collapse navbar-collapse">
3939
<div class="navbar-text navbar-right">
40+
<% admins_only do %>
41+
<%= link_to "Admin", admin_root_path %>
42+
<% end %>
4043
Signed in as <%= current_user.email %> &middot;
4144
<%= link_to "Sign out", destroy_user_session_path, method: :delete %>
4245
</div>

ticketee/config/routes.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
Rails.application.routes.draw do
22
namespace :admin do
3+
root "base#index"
34
resources :users
45
end
56

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(:user, :admin) }
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)