Skip to content

Instantly share code, notes, and snippets.

@cupakromer
Last active May 19, 2020 10:20
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cupakromer/6680452 to your computer and use it in GitHub Desktop.
Save cupakromer/6680452 to your computer and use it in GitHub Desktop.
Rails 4 Standard CRUD Controller
# No flash messages are set with this controller.
#
# This is a fairly basic / bare controller which does only the basic CRUD.
class BooksController < ApplicationController
respond_to :html, :xml, :json
before_action :set_book, only: [:show, :edit, :update, :destroy]
def index
respond_with @books = Book.all
end
def show
respond_with @book
end
def new
respond_with @book = Book.new
end
def edit
respond_with @book
end
def create
respond_with @book = Book.create(book_params)
end
def update
@book.update(book_params)
respond_with @book
end
def destroy
# Destroy returns the object (i.e. self); though I believe Mongoid returns a boolean - need to double check this
@book.destroy
respond_with @book.destroy
end
private
def set_book
@book = Book.find(params[:id])
end
def book_params
params[:book].permit(:title, :isbn, :price)
end
end
class BooksController < ApplicationController
respond_to :html, :xml, :json
before_action :set_book, only: [:show, :edit, :update, :destroy]
def index
respond_with @books = Book.all
end
def show
respond_with @book
end
def new
respond_with @book = Book.new
end
def edit
respond_with @book
end
def create
# In order to set the flash, we need to know if the create was successful.
# Since `create` always returns an object we won't know if it's successful,
# without re-validating or checking the errors.
#
# The cleaner way is to just initialize a new object, then check the call
# to save; which will be truthy on a valid model and successful save.
@book = Book.new(book_params)
if @book.save
flash[:notice] = "Book was successfully created."
end
respond_with @book
end
def update
# Update returns truthy if the model was valid and the save successful.
# So it's fine to just make the call.
if @book.update(book_params)
flash[:notice] = "Book was successfully updated."
end
# respond_with is very smart / magical. It knows that only on a
# successful update should the location option be used as a
# redirect.
respond_with @book, location: root_url
end
def destroy
@book.destroy
flash[:notice] = "Book was successfully destroyed."
respond_with @book
end
private
def set_book
@book = Book.find(params[:id])
end
def book_params
params[:book].permit(:title, :isbn, :price)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment