Last active
August 29, 2015 14:06
-
-
Save mdunbavan/67a7e2d9311721681563 to your computer and use it in GitHub Desktop.
rails book show view
This file contains hidden or 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
// Show view | |
<p id="notice"><%= notice %></p> | |
<p> | |
<strong>Title:</strong> | |
<%= @book.title %> | |
</p> | |
<p> | |
<strong>Synopsis:</strong> | |
<%= @book.synopsis %> | |
</p> | |
<p> | |
<strong>Body:</strong> | |
<%= @book.body %> | |
</p> | |
<%= @book.author.name %> | |
<%= @book.author.biography %> | |
<%= link_to 'Edit', edit_book_path(@book) %> | | |
<%= link_to 'Back', books_path %> | |
// Books controller | |
class BooksController < ApplicationController | |
before_action :set_book, only: [:show, :edit, :update, :destroy] | |
before_filter :authenticate_user!, only: [:new, :edit, :update, :destroy] | |
def index | |
@books = Book.order('created_at DESC').all | |
end | |
def show | |
# @book = Book.find(params[:id]) | |
@book = Book.friendly.find(params[:id]) | |
end | |
# GET /books/new | |
def new | |
@book = Book.new | |
@book.build_author | |
end | |
# GET /books/1/edit | |
def edit | |
end | |
def create | |
@book = Book.new(book_params) | |
respond_to do |format| | |
if @book.save | |
format.html { redirect_to @book, notice: 'Book was successfully created.' } | |
format.json { render action: 'show', status: :created, location: @book } | |
else | |
format.html { render action: 'new' } | |
format.json { render json: @book.errors, status: :unprocessable_entity } | |
end | |
end | |
end | |
private | |
# Use callbacks to share common setup or constraints between actions. | |
def set_book | |
# @book = Book.find(params[:id]) | |
@book = Book.friendly.find(params[:id]) | |
end | |
# Never trust parameters from the scary internet, only allow the white list through. | |
def book_params | |
params.require(:book).permit(:title, :synopsis, :body, :age, :publisher, :jacket_cover, author_attributes: [:name,:biography]) | |
end | |
end | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment