Created
April 5, 2016 22:15
-
-
Save mfifth/9e6014c71e0da833d484aba5210964b6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TicketsController < ApplicationController | |
before action :validate_user, only: [:create, :new] | |
before_action :set_project | |
before_action :set_ticket, only: [:show, :edit, :update, :destroy] | |
def new | |
@ticket = @project.tickets.build | |
end | |
def destroy | |
@ticket.destroy | |
flash[:notice] = 'Ticket has been deleted.' | |
redirect_to @project | |
end | |
def show | |
end | |
def edit | |
end | |
def update | |
if @ticket.update(ticket_params) | |
flash[:notice] = 'Ticket has been updated.' | |
redirect_to [@project, @ticket] | |
else | |
flash.now[:alert] = 'Ticket has not been updated.' | |
render 'edit' | |
end | |
end | |
def create | |
@ticket = @project.tickets.build(ticket_params) | |
@ticket.author = current_user | |
if @ticket.save | |
flash[:notice] = 'Ticket has been created.' | |
redirect_to [@project, @ticket] | |
else | |
flash.now[:alert] = 'Ticket has not been created.' | |
render 'new' | |
end | |
end | |
private | |
def set_ticket | |
@ticket = @project.tickets.find(params[:id]) | |
end | |
def set_project | |
@project = Project.find(params[:project_id]) | |
end | |
def ticket_params | |
params.require(:ticket).permit(:name, :description) | |
end | |
def validate_user | |
if current_user = nil | |
redirect_to root_path | |
flash[:notice] = 'You must be signed in to use this feature' | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment