Skip to content

Commit

Permalink
Add update action to complete items.
Browse files Browse the repository at this point in the history
  • Loading branch information
Alejandro Ventura Contreras committed Apr 15, 2019
1 parent 3dc1982 commit 7f88b45
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
21 changes: 21 additions & 0 deletions app/controllers/items_controller.rb
@@ -1,11 +1,14 @@
class ItemsController < ApplicationController
before_action :set_item, only: [:update]

def index
items = Item.pending
render json: items
end

def create
item = Item.new(item_params)

if item.save
serialized_data = ActiveModelSerializers::Adapter::Json.new(
ItemSerializer.new(item)
Expand All @@ -16,8 +19,26 @@ def create
end
end

# PATCH/PUT /items/1
def update
if @item.update(item_params)
serialized_data = ActiveModelSerializers::Adapter::Json.new(
ItemSerializer.new(Item.pending)
).serializable_hash

ActionCable.server.broadcast 'items_channel', serialized_data
head :ok
else
render :edit
end
end

private

def set_item
@item = Item.find(params[:id])
end

def item_params
params.require(:item).permit(:description, :is_done)
end
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
@@ -1,5 +1,5 @@
Rails.application.routes.draw do
resources :items, only: [:index, :create]
resources :items, only: [:index, :create, :update]
mount ActionCable.server, at: '/cable'
# For details on the DSL available within this file, see http://guides.rubyonrails.org/routing.html
end

0 comments on commit 7f88b45

Please sign in to comment.