Skip to content

Instantly share code, notes, and snippets.

@mdunbavan
Created September 16, 2014 19:45
Show Gist options
  • Save mdunbavan/0dd592b6a8878a664ab0 to your computer and use it in GitHub Desktop.
Save mdunbavan/0dd592b6a8878a664ab0 to your computer and use it in GitHub Desktop.
Rails controllers for gallery on books
class BooksController < ApplicationController
before_action :set_book, only: [:show, :edit, :update, :destroy]
before_filter :authenticate_user!, only: [:new, :edit, :update, :destroy]
def index
@books = Book.order('created_at DESC').all
end
def show
@book = Book.friendly.find(params[:id])
@gallery = @book.gallery
end
def new
@book = Book.new
@book.build_author
@gallery = Gallery.new
@book.build_gallery
end
def edit
end
def create
@book = Book.new(book_params)
respond_to do |format|
if @book.save
format.html { redirect_to @book, notice: 'Book was successfully created.' }
format.json { render action: 'show', status: :created, location: @book }
else
format.html { render action: 'new' }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @book.update(book_params)
format.html { redirect_to @book, notice: 'Book was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @book.errors, status: :unprocessable_entity }
end
end
end
def destroy
@book.destroy
respond_to do |format|
format.html { redirect_to books_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_book
# @book = Book.find(params[:id])
@book = Book.friendly.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def book_params
params.require(:book).permit(:title, :synopsis, :body, :age, :publisher, :jacket_cover, author_attributes: [:name,:biography], gallery_attributes: [:image] )
end
end
class GalleriesController < ApplicationController
before_action :set_gallery, only: [:show, :edit, :update, :destroy]
def index
@galleries = Gallery.all
end
def show
@gallery = Gallery.find(params[:id])
@images = @gallery.images
end
def new
@gallery = Gallery.new
end
def edit
end
def create
@gallery = Gallery.new(gallery_params)
@image = Image.create( image_params )
respond_to do |format|
if @gallery.save
format.html { redirect_to @gallery, notice: 'Gallery was successfully created.' }
format.json { render action: 'show', status: :created, location: @gallery }
else
format.html { render action: 'new' }
format.json { render json: @gallery.errors, status: :unprocessable_entity }
end
end
end
def update
respond_to do |format|
if @gallery.update(gallery_params)
format.html { redirect_to @gallery, notice: 'Gallery was successfully updated.' }
format.json { head :no_content }
else
format.html { render action: 'edit' }
format.json { render json: @gallery.errors, status: :unprocessable_entity }
end
end
end
def destroy
@gallery.destroy
respond_to do |format|
format.html { redirect_to galleries_url }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_gallery
@gallery = Gallery.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def gallery_params
params.require(:gallery).permit(:name, :book_id)
end
end
class GalleriesController < ApplicationController
before_action :set_image, only: [:show, :edit, :update, :destroy]
def show
@image = Image.find(params[:id])
end
def index
@images = Image.order('created_at DESC').all
end
def new
@image = Image.new
end
def create
@image = Image.create( image_params )
end
def edit
end
private
# Use callbacks to share common setup or constraints between actions.
def set_image
@image = Image.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def image_params
params.require(:image).permit(:file)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment