Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

updated index #8

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions app/controllers/api/v1/todos_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
class Api::V1::TodosController < ApplicationController

def index
@todo = Todo.all

render json: @todo
end

def show
@todo = Todo.find(params[:id])
end

def create
@todo = Todo.new(todo_params)

if @todo.save
render json: @todo, status: :created
else
render @todo.errors, status: :unprocessable_entity
end
end

def update
@todo = Todo.find(params[:id])

if @todo.update(todo_params)
head :no_content
else
render @todo.errors, status: :unprocessable_entity
end
end

def destroy
@todo = Todo.find(params[:id])
@todo.destroy

head :no_content
end

private

def todo_params
params.permit(:title, :order, :completed)
end
end
3 changes: 3 additions & 0 deletions app/serializers/todo_serializer.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class TodoSerializer < ActiveModel::Serializer
attributes :id, :title, :order, :completed
end
1 change: 1 addition & 0 deletions app/views/todos/index.json.jbuilder
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
json.(@todo, :id, :title, :order, :completed)
7 changes: 7 additions & 0 deletions test/controllers/api/v1/todos_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class Api::V1::TodosControllerTest < ActionController::TestCase
# test "the truth" do
# assert true
# end
end