From d7fd18ab619303d9ef8b955a00b1c193ce82ff2e Mon Sep 17 00:00:00 2001 From: Monique Date: Fri, 12 Aug 2016 14:27:20 -0400 Subject: [PATCH] Views for polls fixed. has_admin_rights method created under users helper so that only admins can view editing and detroy functions --- app/controllers/polls_controller.rb | 8 ++++++++ app/helpers/users_helper.rb | 3 +++ app/views/polls/index.html.haml | 11 +++++------ 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/app/controllers/polls_controller.rb b/app/controllers/polls_controller.rb index 4f66a41..4c0fb05 100644 --- a/app/controllers/polls_controller.rb +++ b/app/controllers/polls_controller.rb @@ -1,6 +1,7 @@ class PollsController < ApplicationController load_and_authorize_resource only: [:new, :create, :edit, :update, :destroy, :published] + before_action :admin_user, only: [:new, :create, :edit, :update, :destroy] before_action :authenticate_user! # GET /polls @@ -100,9 +101,16 @@ def answer end private + def poll_params params.require(:poll).permit( :question, :published ) end + +def admin_user + redirect_to(root_url) + unless current_user.admin? + end +end end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index b2ea9a4..06609ad 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -7,4 +7,7 @@ def display_name(user) user.hide_last_name? ? first_name(user) : user.name end + def has_admin_rights + current_user.admin? + end end diff --git a/app/views/polls/index.html.haml b/app/views/polls/index.html.haml index e06724b..91f9f2a 100644 --- a/app/views/polls/index.html.haml +++ b/app/views/polls/index.html.haml @@ -5,10 +5,9 @@ .poll{'data-id' => poll.id} %h2= poll.question .publish - = if current_user.admin? - = render :partial => "polls/publish_link", :locals => {:poll => poll}.actions - = link_to 'Edit', edit_poll_path(poll) - = link_to 'Destroy', poll, method: :delete, data: { confirm: 'Are you sure?' } - = link_to 'Show', poll + = render :partial => "polls/publish_link", :locals => {:poll => poll}.actions + = link_to 'Edit', edit_poll_path(poll) if has_admin_rights + = link_to 'Destroy', poll, method: :delete, data: { confirm: 'Are you sure?' } if has_admin_rights + = link_to 'Show', poll -= link_to 'New Poll', new_poll_path if current_user.admin? += link_to 'New Poll', new_poll_path if has_admin_rights