Skip to content

Instantly share code, notes, and snippets.

@foucist
Created November 26, 2014 15:59
Show Gist options
  • Select an option

  • Save foucist/b546eb700a3e2c828281 to your computer and use it in GitHub Desktop.

Select an option

Save foucist/b546eb700a3e2c828281 to your computer and use it in GitHub Desktop.
class PhotosController < ApplicationController
respond_to :json
def index
respond_with load_photos
end
def show
respond_with load_photo
end
def new
respond_with build_photo
end
def create
build_photo
respond_with save_photo or render json: 'new'
end
def edit
load_photo
respond_with build_photo
end
def update
load_photo
build_photo
respond_with save_photo or render json: 'edit'
end
def destroy
load_photo
respond_with @photo.destroy
end
private
def load_photos
@photos ||= photo_scope.to_a
end
def load_photo
@photo ||= photo_scope.find(params[:id])
end
def build_photo
@photo ||= photo_scope.build
@photo.attributes = photo_params
end
def save_photo
if @photo.save
redirect_to @photo
end
end
def photo_params
photo_params = params[:photo]
photo_params ? photo_params.permit(:caption, :position, :private) : {}
end
def photo_scope
Photo.all
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment