iT邦幫忙

2017 iT 邦幫忙鐵人賽
DAY 17
0
Modern Web

RRRR的世界 (Ruby on Rails + React + Redux)系列 第 17

Day 17, Reading List - Rails部分-2

大家新年快樂,2017第一天!
這是同場加映(?好啦是補上controller跟Rails的TDD

Controller

首先,先看看我把之前scaffold person的controller改寫的結果:

class BooksController < ApplicationController
  before_action :set_book, only: [:show, :edit, :update, :destroy]

  # GET /books
  # GET /books.json
  def index
    @books = Book.includes(:author)
  end

  # GET /books/1
  # GET /books/1.json
  def show
  end

  # GET /books/new
  def new
    @book = Book.new
    @author = @book.build_author
  end

  # GET /books/1/edit
  def edit
  end

  # POST /books
  # POST /books.json
  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 :show, status: :created, location: @book }
      else
        format.html { render :new }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end

  # PATCH/PUT /books/1
  # PATCH/PUT /books/1.json
  def update
    respond_to do |format|
      if @book.update(book_params)
        format.html { redirect_to @book, notice: 'Book was successfully updated.' }
        format.json { render :show, status: :ok, location: @book }
      else
        format.html { render :edit }
        format.json { render json: @book.errors, status: :unprocessable_entity }
      end
    end
  end

  # DELETE /books/1
  # DELETE /books/1.json
  def destroy
    @book.destroy
    respond_to do |format|
      format.html { redirect_to books_url, notice: 'Book was successfully destroyed.' }
      format.json { head :no_content }
    end
  end

  private
    # Use callbacks to share common setup or constraints between actions.
    def set_book
      @book = Book.find(params[:id])
    end

    # Never trust parameters from the scary internet, only allow the white list through.
    def book_params
      params.require(:book).permit(:name, :status, author_attributes: [:name])
    end
end

幾乎不變,person -> book, people -> books, Person -> Book

    def book_params
      params.require(:book).permit(:name, :status, author_attributes: [:name])
    end

這裡變比較多,多了一個author_attributes做nested。
然後model Book要加上
accepts_nested_attributes_for :author

可能有些bug,但是邊寫邊改!

Jbuilder json

這邊有三支:

#index.json.jbuilder
json.array! @books, partial: 'books/book', as: :book
#show.json.jbuilder
json.partial! "books/book", book: @book
#_book.json.jbuilder
json.extract! book, :id, :name, :status, :created_at, :updated_at
json.author do
  json.name book.author.name
end
json.url book_url(book, format: :json)

這三隻主要回傳json用的,讓前端的react去parse

DB seed

先灌點假資料看看

author = Author.create!(name: '睏睏')
author2 = Author.create!(name: '寶寶')
books = Book.create([
  {name: '稱過三十天不思議', status: Book.statuses[:unread], author: author},
  {name: '我為什麼要鐵人幫', status: Book.statuses[:unread], author: author2},
  {name: '我為什麼要寫程式', status: Book.statuses[:unread], author: author}
])

記得要下 rails db:seed 指令

先發文,之後補上rspec!


上一篇
Day 16, Reading List - Rails的部分
下一篇
Day 18, Reading List - Rails部分-3
系列文
RRRR的世界 (Ruby on Rails + React + Redux)30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言