2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Ruby on RailsをCursor(VSCode)でデバッグ実行する

Posted at

背景

Railsを学習する過程で、効率的なデバッグ方法を模索していました。特にDockerコンテナ内で動作するRailsアプリケーションをVSCode(Cursor)からデバッグできると、開発効率が大幅に向上します。この記事では、その設定方法をステップバイステップで解説します。

必要なコード

Dockerfile

dockerのビルド時にbundle installをするよう作成します。

FROM ruby:3.2.3

WORKDIR /app
COPY Gemfile* ./
RUN bundle install
COPY . /app

EXPOSE 3000

compose.yaml

services:
  api:
    build:
      context: .
      dockerfile: Dockerfile
    container_name: container_api
    ports:
      - "3000:3000"
    tty: true
    volumes:
      - .:/app

手順

1. "Dev Containers" 拡張機能をインストールする

Cursor(VSCode)で「Dev Containers」の拡張機能をインストールします。この拡張機能により、Dockerコンテナ内にログインしてファイルの編集、コマンドの実行などができるようになります。

screenshot 2025-04-07 at 9.41.37.png

2. Gemfileの修正

Gemfileのgroup :development doブロックにgem "ruby-debug-ide"を追記します。すでに追加されている場合は、この手順をスキップしてください。

...
group :development do
  # Speed up commands on slow machines / big apps [https://github.com/rails/spring]
  ...
  gem "ruby-debug-ide"
end
...

3. Dockerをビルドして起動する

ターミナルで以下のコマンドを実行し、Dockerコンテナをビルドして起動します。
このときビルド時にbundle installを実行するので、ruby-debug-ideがinstallされます。

docker compose build
docker compose up

4. Dev Containersでdockerにログインする

VSCode(Cursor)の左下にあるリモート接続アイコンをクリックし、「Attach to Running Container」を選択します。
表示されるコンテナ一覧から「container_api」を選択します。

screenshot 2025-04-07 at 9.48.38.png

screenshot 2025-04-07 at 9.53.10.png

5. "Ruby" 拡張機能をインストールする

コンテナにログイン後、Railsのデバッグ実行を有効化するため、「Ruby」の拡張機能をインストールします。

screenshot 2025-04-07 at 9.58.55.png

6. launch.jsonの作成

.vscodeディレクトリがない場合は作成し、その中にlaunch.jsonファイルを作成します。

mkdir -p .vscode

.vscode/launch.jsonに以下の内容を記述します。

{
    "version": "0.2.0",
    "configurations": [
        {
            "name": "Rails server",
            "type": "Ruby",
            "request": "launch",
            "program": "${workspaceRoot}/bin/rails",
            "args": [
                "server",
                "-b",
                "0.0.0.0",
                "-p",
                "3000"
            ],
        }
    ]
}

7. デバッグ実行

VSCode(Cursor)のデバッグアイコンを選択し、「Rails server」を選択して実行ボタンをクリックします。デバッグコンソールにRailsサーバーの起動ログが表示されることを確認します。

screenshot 2025-04-07 at 10.02.23.png

これで、ブレークポイントを設定してデバッグ実行ができるようになりました🎉
コード内の任意の行の左側をクリックしてブレークポイントを設定し、そのコードが実行されると処理が一時停止します。

screenshot 2025-04-07 at 10.09.22.png

変数の中身は、デバッグエリアかデバックコンソールの入力エリアに変数名を入力して確認することができます。

screenshot 2025-04-07 at 10.11.45.png

まとめ

Cursor(VSCode)でRuby on Railsアプリケーションをデバッグ実行する方法を解説しました。Dev Containers拡張機能を使用することで、Dockerコンテナ内のRailsアプリケーションを効率的にデバッグできます😊

デバッグ機能を活用することで、Railsアプリケーションの開発効率が向上し、複雑なバグの解決も容易になります。

今回紹介した方法は一例であり、プロジェクトの要件や開発環境に合わせてカスタマイズしてください🙆‍♂️

2
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
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?