RailsによるアジャイルWebアプリケーション開発

第20章 Action DispatchとAction Controller

参考

【翻訳】RESTのベストプラクティス | POSTD

ルート

  • config/routes.rb
Rails.application.routes.draw do
  resources :products do
    resources :reviews
  end
end
  • ルート結果
% ./bin/rake routes
             Prefix Verb   URI Pattern                                      Controller#Action
    product_reviews GET    /products/:product_id/reviews(.:format)          reviews#index
                    POST   /products/:product_id/reviews(.:format)          reviews#create
new_product_review GET    /products/:product_id/reviews/new(.:format)      reviews#new
edit_product_review GET    /products/:product_id/reviews/:id/edit(.:format) reviews#edit
     product_review GET    /products/:product_id/reviews/:id(.:format)      reviews#show
                    PATCH  /products/:product_id/reviews/:id(.:format)      reviews#update
                    PUT    /products/:product_id/reviews/:id(.:format)      reviews#update
                    DELETE /products/:product_id/reviews/:id(.:format)      reviews#destroy
           products GET    /products(.:format)                              products#index
                    POST   /products(.:format)                              products#create
        new_product GET    /products/new(.:format)                          products#new
       edit_product GET    /products/:id/edit(.:format)                     products#edit
            product GET    /products/:id(.:format)                          products#show
                    PATCH  /products/:id(.:format)                          products#update
                    PUT    /products/:id(.:format)                          products#update
                    DELETE /products/:id(.:format)                          products#destroy

浅いルート

reviewの更新操作はproductのパスに連なっていない。

  • config/routes.rb
Rails.application.routes.draw do
  resources :products, shallow: true do
    resources :reviews
  end
end
  • 結果
% ./bin/rake routes
            Prefix Verb   URI Pattern                                 Controller#Action
   product_reviews GET    /products/:product_id/reviews(.:format)     reviews#index
                   POST   /products/:product_id/reviews(.:format)     reviews#create
new_product_review GET    /products/:product_id/reviews/new(.:format) reviews#new
       edit_review GET    /reviews/:id/edit(.:format)                 reviews#edit
            review GET    /reviews/:id(.:format)                      reviews#show
                   PATCH  /reviews/:id(.:format)                      reviews#update
                   PUT    /reviews/:id(.:format)                      reviews#update
                   DELETE /reviews/:id(.:format)                      reviews#destroy
          products GET    /products(.:format)                         products#index
                   POST   /products(.:format)                         products#create
       new_product GET    /products/new(.:format)                     products#new
      edit_product GET    /products/:id/edit(.:format)                products#edit
           product GET    /products/:id(.:format)                     products#show
                   PATCH  /products/:id(.:format)                     products#update
                   PUT    /products/:id(.:format)                     products#update
                   DELETE /products/:id(.:format)                     products#destroy

リクエストの処理

  • renedrは処理を終了させないので複数通るように記述しないこと
  • jsonpを返したいばあいは、以下のようにする


Working with JSONP | jQuery Learning Center

  render json: @data, callback: params[:callback]
  • renderとredirect_toは動作の意図を考慮して適切に使い分ける

Session

デフォルトはcookie
ActionDispatch::Session::CookieStore


DBを使用する場合はgemを

rails/activerecord-session_store · GitHub

  • 重要な情報を保存しない
  • 容量は4k
  • セッションの失効とクリーンアップ

フラッシュ

  • nowとそうではないのをきちんと使い分けなにと思いがけないところで思いがけないメッセージが出ることも
  • keepは知らなかった

ActionDispatch::Flash::FlashHash

フィルター

  • Rails4 では before_ や after_ に続くのはfilterじゃなくてactionなので注意

第21章 Action View

  • debug

ActionView::Helpers::DebugHelper

  • 正規表現について、^$と\A\zは使い分けないとエラーになる

The provided regular expression is using multiline anchors (^ or $), which may present a security risk. Did you mean to use \A and \z, or forgot to add the :multiline => true option?

  • app/models/picture.rb
  validates :content_type, format: {
    with: /\Aimage/i, message: "--- アップロードできるのは画像だけです"
  }
  • attributes を変更した場合は、Strong Paremeter を見直すことを忘れずに
  • app/controllers/pictures_controller.rb
  def picture_params
    params.require(:picture).permit(:comment, :upload_picture)
  end
  • ヘルパー
    • 引数が変更になっているものも
[1] pry(main)> helper.distance_of_time_in_words(Time.now, Time.now + 38, include_seconds: true)
=> "30秒前後"
[2] pry(main)> helper.distance_of_time_in_words(Time.now, Time.now + 38, include_seconds: false)
=> "1分"
[3] pry(main)> helper.excerpt("あいうえおかきくけこ", "", radius: 2)
=> "...うえおかき..."
  • button_to と linkt_to は違う
    • button_to は自己完結フォームを作製する
  • レイアウトと部分テンプレート
    • content_for を使う事により yield で表示する内容を場面ごとで変更できる
    • partial を使う事により使えるオプションがある

ActionView::PartialRenderer