Skip to content

Commit

Permalink
Section 7.4.7: Add ability to edit and update users
Browse files Browse the repository at this point in the history
  • Loading branch information
Ryan Bigg committed Dec 2, 2014
1 parent 890d6bf commit a0f4e05
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 0 deletions.
26 changes: 26 additions & 0 deletions ticketee/app/controllers/admin/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Admin::UsersController < Admin::BaseController
before_action :set_user, only: [:show, :edit, :update, :destroy]

def index
@users = User.order(:email)
end
Expand All @@ -21,12 +23,36 @@ def create
end
end

def edit
end

def update
if params[:user][:password].blank?
params[:user].delete(:password)
params[:user].delete(:password_confirmation)
end
if @user.update(user_params)
flash[:notice] = "User has been updated."
redirect_to admin_users_path
else
flash[:alert] = "User has not been updated."
render "edit"
end
end

private

def set_user
@user = User.find(params[:id])
end

def user_params
params.require(:user).permit(:name,
:email,
:password,
:password_confirmation,
:admin)
end


end
3 changes: 3 additions & 0 deletions ticketee/app/views/admin/users/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h2>Editing a User</h2>

<%= render "form" %>
3 changes: 3 additions & 0 deletions ticketee/app/views/admin/users/show.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<h2><%= @user %></h2>

<%= link_to "Edit User", edit_admin_user_path(@user) %>
33 changes: 33 additions & 0 deletions ticketee/spec/features/admin/editing_users_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
require "rails_helper"

feature "Editing a user" do
let!(:admin_user) { FactoryGirl.create(:admin_user) }
let!(:user) { FactoryGirl.create(:user) }

before do
login_as(admin_user)
visit "/"
click_link "Admin"
click_link "Users"
click_link user.email
click_link "Edit User"
end

scenario "Updating a user's details" do
fill_in "Email", with: "[email protected]"
click_button "Update User"

expect(page).to have_content("User has been updated.")

expect(page).to have_content("[email protected]")
expect(page).to_not have_content(user.email)
end

scenario "Toggling user's admin ability" do
check "Is an admin?"
click_button "Update User"

expect(page).to have_content("User has been updated.")
expect(page).to have_content("#{user.email} (Admin)")
end
end

0 comments on commit a0f4e05

Please sign in to comment.