Skip to content

Instantly share code, notes, and snippets.

@samseen
Created September 26, 2016 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samseen/08a4dd71f4eef01ab3fccd06c3342621 to your computer and use it in GitHub Desktop.
Save samseen/08a4dd71f4eef01ab3fccd06c3342621 to your computer and use it in GitHub Desktop.
In the users_controller.rb -->{I intentionally commented out line 56 because as that reduced the error. I added Line 57 as a work around.}
Rails.application.routes.draw do
#resources :users
root 'welcome#home'
get 'about', to: 'welcome#about'
resources :articles
get 'signup', to: 'users#new'
resources :users, except: [:new]
get 'login', to: 'sessions#new'
post 'login', to: 'sessions#create'
delete 'logout', to: 'sessions#destroy'
# The priority is based upon order of creation: first created -> highest priority.
# See how all your routes lay out with "rake routes".
# You can have the root of your site routed with "root"
# root 'welcome#index'
# Example of regular route:
# get 'products/:id' => 'catalog#view'
# Example of named route that can be invoked with purchase_url(id: product.id)
# get 'products/:id/purchase' => 'catalog#purchase', as: :purchase
# Example resource route (maps HTTP verbs to controller actions automatically):
# resources :products
# Example resource route with options:
# resources :products do
# member do
# get 'short'
# post 'toggle'
# end
#
# collection do
# get 'sold'
# end
# end
# Example resource route with sub-resources:
# resources :products do
# resources :comments, :sales
# resource :seller
# end
# Example resource route with more complex sub-resources:
# resources :products do
# resources :comments
# resources :sales do
# get 'recent', on: :collection
# end
# end
# Example resource route with concerns:
# concern :toggleable do
# post 'toggle'
# end
# resources :posts, concerns: :toggleable
# resources :photos, concerns: :toggleable
# Example resource route within a namespace:
# namespace :admin do
# # Directs /admin/products/* to Admin::ProductsController
# # (app/controllers/admin/products_controller.rb)
# resources :products
# end
end
class UsersController < ApplicationController
before_action :set_user, only: [:edit, :update, :show]
before_action :require_same_user, only: [:edit, :update, :destroy]
before_action :require_admin, only: [:destroy]
def index
@users = User.paginate(page: params[:page], per_page: 5)
end
def new
@user = User.new
end
def create
@user = User.new(user_params)
if @user.save!
session[:user_id] = @user.id
flash[:success] = "Welcome to the alpha blog #{@user.username}"
redirect_to user_path(@user)
else
render 'new'
end
end
def edit
end
def update
if @user.update(user_params)
flash[:success] = "Your account was updated successfully"
redirect_to articles_path
else
render 'edit'
end
end
def show
@user_articles = @user.articles.paginate(page: params[:page], per_page: 5)
end
def destroy
@user = User.find(params[:id])
@user.destroy
flash[:danger] = "User and all articles created by user have been deleted"
redirect_to users_path
end
private
def user_params
params.require(:user).permit(:username, :email, :password)
end
def set_user
@user = User.find(params[:id])
end
def require_same_user
#if current_user != @user and !current_user.admin?
if current_user.try(:admin?)
flash[:danger] = "You can only edit your own account"
redirect_to root_path
end
end
#def require_same_user
#if current_user.try(:admin?)
#flash[:danger] = "You can only edit your own account"
#redirect_to root_path
#end
#end
def require_admin
if logged_in? and !current_user.admin?
flash[:danger] = "Only admin users can perform that action"
redirect_to root_path
end
end
end
require 'test_helper'
class UsersControllerTest < ActionController::TestCase
setup do
@user = users(:one)
end
test "should get index" do
get :index
assert_response :success
assert_not_nil assigns(:users)
end
test "should get new" do
get :new
assert_response :success
end
test "should create user" do
assert_difference('User.count') do
post :create, user: { email: "sample_#{rand(100)}@outlook.com", username: "sample_#{rand(100)}", password: ""}
end
assert_redirected_to user_path(assigns(:user))
end
test "should show user" do
get :show, id: @user
assert_response :success
end
test "should get edit" do
get :edit, id: @user
assert_response :success
end
test "should update user" do
patch :update, id: @user, user: { email: @user.email, username: @user.username, password: @user.password }
assert_redirected_to user_path(assigns(:user))
end
test "should destroy user" do
assert_difference('User.count', -1) do
delete :destroy, id: @user
end
assert_redirected_to users_path
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment