Skip to content

Instantly share code, notes, and snippets.

@mrcasals
Created October 6, 2011 18:17
Show Gist options
  • Save mrcasals/1268166 to your computer and use it in GitHub Desktop.
Save mrcasals/1268166 to your computer and use it in GitHub Desktop.
Rails Controllers with DataMapper
# This class is responsible for the Books REST interface.
#
class BooksController < ApplicationController
# Get a book by the id
before_filter :get_book, only: [:edit, :update, :show, :destroy]
# Renders the form to create a new Book.
#
def new
@book = Book.new
end
# Creates a new Book from the params and redirects to edit view.
#
def create
@book = Book.create(params[:book])
redirect_to book_path(@book)
end
# Renders the form for a given book.
#
def edit
end
# Updates a Book from the params and redirects to edit view.
#
def update
@book.update(params[:book])
redirect_to edit_book_path(@book)
end
# Renders all books
#
def index
@books = Book.all
end
# Renders a Book
#
def show
end
# Destroys the Book object from database
#
def destroy
@book.destroy
redirect_to action: :index
end
private
def get_book
@book = Book.get(params[:id])
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment