Created
November 26, 2014 15:59
-
-
Save foucist/b546eb700a3e2c828281 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 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