Created
June 9, 2015 15:31
-
-
Save lulalala/02b9e75a54a96ad20d4f to your computer and use it in GitHub Desktop.
Simple controller
This file contains 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
# 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