:update
General Setup & Layout
Example Setup:
class ItemsController < ApplicationController
before_action :find_item, only: [:show, :update, :destroy]
def update
# LOCAL HANDLING
##################
# Check if the item belongs to the current company (or another ownership/permission logic)
unless @item.company == get_current_company
return render json: { error: 'Not authorized' }, status: :forbidden
end
# Additional business logic checks, if needed
# For instance: is the item currently checked out, or has it reached a certain status?
# ...
# ABSTRACTED/REUSABLE HANDLING (e.g. Concerns, etc.)
##################
# CONCERNS
# Maybe there's a concern for updating certain attributes or logging changes?
# @item = handle_update_logic(@item)
# OTHER (?)
# ...
# EXECUTE & RESPOND
##################
# Update the item and render response
if @item.update(item_params)
render json: @item
else
render json: @item.errors, status: :unprocessable_entity
end
end
# ... Other actions ...
private
def item_params
params.require(:item).permit(:name, :description, :price, ...other permitted attributes...)
end
def find_item
@item = Item.find(params[:id])
rescue ActiveRecord::RecordNotFound
render json: { error: 'Item not found' }, status: :not_found
end
end
Last updated