Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.