Skip to content

Instantly share code, notes, and snippets.

@lulalala
Created June 9, 2015 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lulalala/02b9e75a54a96ad20d4f to your computer and use it in GitHub Desktop.
Save lulalala/02b9e75a54a96ad20d4f to your computer and use it in GitHub Desktop.
Simple controller
# This is generated using toy_scaffold,
# and is meant for learning purposes only.
# It aims for more clarity for beginners, therefore
# sacrifices some best practices and DRY principle.
class BooksController < ApplicationController
# GET /books
# View file is at app/views/books/index.html.erb
def index
@books = Book.all
end
# GET /books/1
# View file is at app/views/books/show.html.erb
def show
@book = Book.find(params[:id])
end
# GET /books/new
# View file is at app/views/books/new.html.erb
def new
@book = Book.new
end
# GET /books/1/edit
# View file is at app/views/books/edit.html.erb
def edit
@book = Book.find(params[:id])
end
# POST /books
def create
@book = Book.new(book_params)
if @book.save
redirect_to @book, notice: 'Book was successfully created.'
else
render :new
end
end
# PATCH/PUT /books/1
def update
@book = Book.find(params[:id])
if @book.update(book_params)
redirect_to @book, notice: 'Book was successfully updated.'
else
render :edit
end
end
# DELETE /books/1
def destroy
@book = Book.find(params[:id])
@book.destroy
redirect_to books_url, notice: 'Book was successfully destroyed.'
end
private
# Only allow a trusted parameter "white list" through.
def book_params
params.require(:book).permit(:title)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment