-
-
Save thorsteneckel/5771ec212d108280be23 to your computer and use it in GitHub Desktop.
Rails 4 Standard CRUD 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 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