Skip to content

Instantly share code, notes, and snippets.

@novohispano
Created September 14, 2015 14:18
Show Gist options
  • Save novohispano/4f24c648fb9c2392020d to your computer and use it in GitHub Desktop.
Save novohispano/4f24c648fb9c2392020d to your computer and use it in GitHub Desktop.
class Api::V1::ItemsController < ApplicationController
respond_to :json, :xml
before_action :authenticate!
 
def index
respond_with Item.all
end
 
def show
respond_with Item.find(params[:id])
end
 
def create
respond_with Item.create(item_params)
end
 
def update
respond_with Item.update(params[:id], item_params)
end
 
def delete
respond_with Item.destroy(params[:id])
end
 
private
 
def item_params
params.require(:item).permit(:name, :description, :image_url)
end
def authenticate!
authenticate_or_request_with_http_basic("Please authenticate to use the API") do |email, password|
user = User.find_by(email: email)
return true if user && user.authenticate(password)
head :unauthorized
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment