1
1

パーフェクトRubyonRails備忘録3

Posted at

レンダリング

  • コントローラ内でレンダリングする方法で一番シンプル

    • renderを使う
    class BooksController < ApplicationController
      def show
        # app/views/books/show.html.erbを表示しようとする
        render :show
      end
    
    • renderの場合は省略しても、render アクション名と解釈される。
  • 表示を分ける

    • respond_toを使う
    class BooksController < ApplicationController
      def show
        respond_to do |format|
          # 通常のアクセス GET /books/1
          # render :show, format: :htmlと同じ意味
          format.html
          # jsonを指定してアクセス GET /books/1.json
          # @bookをjson形式に変換して表示
          format.json { render json: @book }
      end
    
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1