Skip to content

Instantly share code, notes, and snippets.

@jhwhite
Created July 11, 2016 01:09
Show Gist options
  • Save jhwhite/3e812bb708c6c6a8a0e965a08548ef50 to your computer and use it in GitHub Desktop.
Save jhwhite/3e812bb708c6c6a8a0e965a08548ef50 to your computer and use it in GitHub Desktop.
#route
get 'tasks/todos-total', to: 'tasks#total_todos'
#tasks_controller
class TasksController < ApplicationController
before_action :set_task, only: [:show, :update, :destroy]
# GET /tasks
def index
@tasks = Task.all
render json: @tasks
end
# GET /tasks/1
def show
render json: @task
end
# POST /tasks
def create
@task = Task.new(task_params)
if @task.save
render json: @task, status: :created, location: @task
else
render json: @task.errors, status: :unprocessable_entity
end
end
# PATCH/PUT /tasks/1
def update
if @task.update(task_params)
render json: @task
else
render json: @task.errors, status: :unprocessable_entity
end
end
# DELETE /tasks/1
def destroy
@task.destroy
end
def total_todos
render json: Task.count
end
private
# Use callbacks to share common setup or constraints between actions.
def set_task
@task = Task.find(params[:id])
end
# Only allow a trusted parameter "white list" through.
def task_params
params.require(:task).permit(:title, :firstname, :lastname, :completed, :order)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment