Skip to content

Instantly share code, notes, and snippets.

@JoeyBy
Created June 3, 2016 23:36
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 JoeyBy/8b4385fb75319fa6ec686d44a7eaa8c7 to your computer and use it in GitHub Desktop.
Save JoeyBy/8b4385fb75319fa6ec686d44a7eaa8c7 to your computer and use it in GitHub Desktop.
class TasksController < ApplicationController
#GET /tasks or using tasks_path
def index
@tasks = Task.all
end
#GET /tasks/new or using new_tasks_path
#this is where our new task form will live
def new
@task = Task.new
end
#POST /tasks or using tasks_path
#once the new task has been submitted using POST, the create action takes over.
def create
#we create a new Task object and pass it paramaters defined at the bottom of the page (see below for description)
@task = Task.new(task_params)
if @task.save
#if newly created task is saved successfuly redirect back to the tasks list page and notify the user.
redirect_to tasks_path, notice "#{@task.title} was created!"
else
#if the newly created task doesn't save render the new task form again.
render :new
end
end
#DELETE /tasks/:id or using task_path(task-to-delete)
def destroy
#find the task that has the ID
@task = Task.find(params[:id])
@task.destroy
#after the task is destroyed return back to the tasks list page.
redirect_to tasks_path
end
protected
# This protected code block is called strong params. Essentially it limits what parameters can be passed to the database. Double check your spelling!
def task_params
params.require(:task).permit(:title, :description)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment