Skip to content

Instantly share code, notes, and snippets.

@thorsteneckel
Forked from cupakromer/books_controller.rb
Last active November 10, 2015 08:36
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 thorsteneckel/5771ec212d108280be23 to your computer and use it in GitHub Desktop.
Save thorsteneckel/5771ec212d108280be23 to your computer and use it in GitHub Desktop.
Rails 4 Standard CRUD Controller
# This is a fairly basic / bare controller which does only the basic CRUD.
class BooksController < ApplicationController
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.require(:title, :price)
params.permit(:title, :isbn, :price)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment