Skip to content

Instantly share code, notes, and snippets.

@mfifth
Last active May 31, 2016 23:51
Show Gist options
  • Select an option

  • Save mfifth/a950909f48f334f4d78b938b41e8f205 to your computer and use it in GitHub Desktop.

Select an option

Save mfifth/a950909f48f334f4d78b938b41e8f205 to your computer and use it in GitHub Desktop.
class Admin::ApplicationController < ApplicationController
before_action :authorize_admin!
def index
@users = User.all
@forums = Forum.all
end
private
def authorize_admin!
authenticate_user!
unless current_user.admin?
redirect_to root_path, alert: "You are not allowed to do that."
end
end
end
<header>
<h1>Admin's Lounge, Please enjoy your stay!</h1>
</header>
<br>
<div class='row'>
<div class='col-md-6'>
<h3>List of Users:</h3>
<% @users.each do |user| %>
<ul class='attributes'>
<li><%= link_to user.email, admin_user_path(user) %></li>
</ul>
<% end %>
<%= link_to 'New User', new_admin_user_path, class: "btn btn-primary" %>
</div>
<div class='col-md-6'>
<h3>List of Forums:</h3>
<% @forums.each do |forum| %>
<ul class='attributes'>
<li><%= link_to forum.title, forum_path(forum) %></li>
</ul>
<% end %>
<%= link_to 'New Forum', new_admin_forum_path, class: "btn btn-primary" %>
</div>
</div>
mfifth:~/workspace $ bundle exec rspec
.F...
Failures:
1) Administrators can successfully update existing users
Failure/Error: <% @forums.each do |forum| %>
ActionView::Template::Error:
undefined method `each' for nil:NilClass
# ./app/views/admin/application/index.html.erb:19:in `_app_views_admin_application_index_html_erb__3305476992675424763_47925760'
# /usr/local/rvm/gems/ruby-2.3.0/gems/warden-1.2.6/lib/warden/manager.rb:35:in `block in call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/warden-1.2.6/lib/warden/manager.rb:34:in `catch'
# /usr/local/rvm/gems/ruby-2.3.0/gems/warden-1.2.6/lib/warden/manager.rb:34:in `call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/etag.rb:24:in `call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/conditionalget.rb:25:in `call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/head.rb:13:in `call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:225:in `context'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:220:in `call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/rack/logger.rb:38:in `call_app'
# /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/rack/logger.rb:20:in `block in call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/rack/logger.rb:20:in `call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/methodoverride.rb:22:in `call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/runtime.rb:18:in `call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/lock.rb:17:in `call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/engine.rb:518:in `call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/railties-4.2.5/lib/rails/application.rb:165:in `call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/urlmap.rb:66:in `block in call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `each'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-1.6.4/lib/rack/urlmap.rb:50:in `call'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-test-0.6.3/lib/rack/mock_session.rb:30:in `request'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-test-0.6.3/lib/rack/test.rb:244:in `process_request'
# /usr/local/rvm/gems/ruby-2.3.0/gems/rack-test-0.6.3/lib/rack/test.rb:58:in `get'
# /usr/local/rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/rack_test/browser.rb:61:in `process'
# /usr/local/rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/rack_test/browser.rb:39:in `block in process_and_follow_redirects'
# /usr/local/rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/rack_test/browser.rb:38:in `times'
# /usr/local/rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/rack_test/browser.rb:38:in `process_and_follow_redirects'
# /usr/local/rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/rack_test/browser.rb:27:in `submit'
# /usr/local/rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/rack_test/form.rb:78:in `submit'
# /usr/local/rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/rack_test/node.rb:62:in `click'
# /usr/local/rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/element.rb:135:in `block in click'
# /usr/local/rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/base.rb:85:in `synchronize'
# /usr/local/rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/element.rb:135:in `click'
# /usr/local/rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/node/actions.rb:40:in `click_button'
# /usr/local/rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/session.rb:699:in `block (2 levels) in <class:Session>'
# /usr/local/rvm/gems/ruby-2.3.0/gems/capybara-2.7.1/lib/capybara/dsl.rb:52:in `block (2 levels) in <module:DSL>'
# ./spec/features/admin_privileges_spec.rb:28:in `block (2 levels) in <top (required)>'
# ------------------
# --- Caused by: ---
# NoMethodError:
# undefined method `each' for nil:NilClass
# ./app/views/admin/application/index.html.erb:19:in `_app_views_admin_application_index_html_erb__3305476992675424763_47925760'
Finished in 1.59 seconds (files took 9.98 seconds to load)
5 examples, 1 failure
Failed examples:
rspec ./spec/features/admin_privileges_spec.rb:22 # Administrators can successfully update existing users
mfifth:~/workspace $
require 'rails_helper'
RSpec.describe "Administrators can" do
let(:admin) { FactoryGirl.create(:user, :admin) }
let!(:user) { FactoryGirl.create(:user) }
before do
login_as(admin)
visit admin_root_path
end
scenario "successfully create new forums" do
click_link "New Forum"
fill_in "Title", with: "Sports"
fill_in "Description", with: "A place where users can grovel and snicker at each other over
sports they have absolutely nothing to do with."
click_button "Create Forum"
expect(page).to have_content "New forum has been created."
end
scenario 'successfully update existing users' do
click_link user.email
click_link "Edit User"
fill_in "Email", with: "[email protected]"
fill_in "Password", with: "frostedflakes"
click_button "Update User"
expect(page).to have_content "User has been updated."
end
end
class Admin::UsersController < Admin::ApplicationController
before_action :set_user, only: [:update, :destroy, :show, :edit]
before_action :authorize_admin!
def index
@users = User.all
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save
flash[:notice] = "User has been successfully created."
redirect_to admin_users_path
else
flash.now[:alert] = "User has not been created."
render 'new'
end
end
def update
@forums = Forum.all
if @user.update(user_params)
flash[:notice] = "User has been successfully updated."
redirect_to admin_users_path
else
flash.now[:alert] = "User has not been updated."
render 'edit'
end
end
def destroy
@user.destroy
flash[:notice] = "User has been deleted."
redirect_to admin_users_path
end
def edit
end
def show
end
private
def authorize_admin!
unless current_user.admin?
redirect_to root_path, alert: "You must be an admin to do that."
end
end
def set_user
@user = User.find(params[:id])
end
def user_params
params.require(:user).permit(:email, :password, :admin)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment