Created
September 14, 2015 14:18
-
-
Save novohispano/4f24c648fb9c2392020d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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