Skip to content

Instantly share code, notes, and snippets.

@davidpaulhunt
Last active August 29, 2015 14:01
Show Gist options
  • Save davidpaulhunt/4b54cd37e5e2ca94864b to your computer and use it in GitHub Desktop.
Save davidpaulhunt/4b54cd37e5e2ca94864b to your computer and use it in GitHub Desktop.
A simple photo model controller with complete CRUD.
class Photo < ActiveRecord::Base
end
class PhotosController < ApplicationController
def new
@new_photo = Photo.new
end
def create
@new_photo = Photo.new(photo_params)
if @new_photo.save
redirect_to photos_path
else
render :new
end
end
def index
@photos = Photo.all
end
def show
@photo = Photo.find(params[:id])
end
def edit
@photo = Photo.find(params[:id])
end
def update
@photo = Photo.find(params[:id])
if @photo.update(photo_params)
redirect_to photo_path
else
render :edit
end
end
def destroy
@photo = Photo.find(params[:id])
if @photo.destroy
redirect_to photos_path
else
render :show
end
end
private
def photo_params
params.require(:photo).permit!
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment