LoginSignup
1
0

More than 1 year has passed since last update.

rails_tutorial chapter7 part1 <忘備録>

Last updated at Posted at 2022-03-23

開発環境でデバッグを表示する

<%= debug(params) if Rails.env.development? %>

/users/1 のURLを有効にするために、routesファイル(config/routes.rb)に次の1行を追加します。

routes.rb
resources :users

以下のroutesが用意される。

1 2 3 4 5
HTTPリクエスト URL アクション 名前付きルート 用途
GET /users index users_path すべてのユーザーを一覧するページ
GET /users/1 show user_path(user) 特定のユーザーを表示するページ
GET /users/new new new_user_path ユーザーを新規作成するページ(ユーザー登録)
POST /users create users_path ユーザーを作成するアクション
GET /users/1/edit edit edit_user_path(user) id=1のユーザーを編集するページ
PATCH /users/1 update user_path(user) ユーザーを更新するアクション
DELETE /users/1 destroy user_path(user) ユーザーを削除するアクション

viewでインスタンスを取り出せるように記述。

users_controller.rb
class UsersController < ApplicationController

  def show
    @user = User.find(params[:id])
    debugger
  end

  def new
  end
end

'debugger'を記述すると、アプリケーションのdebuggerが呼び出された瞬間の状態を確認することができる。

Processing by UsersController#show as HTML
  Parameters: {"id"=>"1"}
   (0.1ms)  SELECT sqlite_version(*)
  ↳ app/controllers/users_controller.rb:3:in `show'
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ?  [["id", 1], ["LIMIT", 1]]
  ↳ app/controllers/users_controller.rb:3:in `show'
Return value is: nil

[1, 10] in /Users/fuko/original_app/sns_app/app/controllers/users_controller.rb
    1: class UsersController < ApplicationController
    2:   def show
    3:   @user = User.find(params[:id])
    4:   debugger
=>  5:   end
    6: 
    7:   def new
    8:   end
    9: 
   10: end
(byebug) 
1
0
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
0