3
1

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環境不要のRailsアプリジェネレータ rails-new

Last updated at Posted at 2024-10-10

Railsプロジェクトを新規に作ろうと思った時、OSアップデートとかの影響で環境構築につまづいちゃうみたいなこと経験ありませんかね (Railsに限った話ではないんだけど)

そんなときに役立つのが、わりかし最近リリースされたrails-newジェネレータです。

rails-newとは?

rails-newはrust製のRailsアプリの雛形ジェネレータです。

このツールの最大の特徴は、ローカルマシンにRubyやRailsの環境がなくても、簡単に新しいRailsプロジェクトの雛形を作成できることです。

リリースページからバイナリをダウンロードして、パスの通ったディレクトリに配置するだけで rails-new コマンドが使用可能になります。

基本的な使い方

rails-newコマンドは、従来のrails newコマンドと同様のオプションをサポートしています。(というか移譲します。)

それに加えて、rubyのバージョンやrailsのバージョンも指定することができます。

➜  workspace rails-new help
A CLI tool to generate a new Rails project

Usage: rails-new [OPTIONS] <ARGS>...
       rails-new [OPTIONS] [ARGS]... <COMMAND>

Commands:
  rails-help  Prints `rails new --help`
  help        Print this message or the help of the given subcommand(s)

Arguments:
  <ARGS>...  arguments passed to `rails new`

Options:
  -u, --ruby-version <RUBY_VERSION>    [default: 3.3.4]
  -r, --rails-version <RAILS_VERSION>  [default: 7.2.0]
  -h, --help                           Print help
  -V, --version                        Print version

使用例:

rails-new myapp

これだけで、Ruby / Rails環境がない場所でも新しいRailsアプリケーションの雛形を生成できます。

Dev Container対応

さらに便利なことに、Railsはバージョン7.2からdevcontainerに対応しています。

以下のようにdevcontainerの設定ファイルを生成するオプションをつけて実行してみましょう。

rails-new myapp --devcontainer

生成されたプロジェクトをDevContainerプラグインを入れたVisual Studio Codeで開くと、「開発コンテナで開き直しますか?」と聞いてくるポップアップがでてきます。

スクリーンショット 2024-10-10 17.56.53.png

これを許可すると必要なRubyやRailsの環境が整った開発コンテナが自動的に立ち上がりvscodeがコンテナ内の環境下で開き直されます。

(コンテナーでサイド開くを押した後、DevContainerが立ち上がってる過程の図)
スクリーンショット 2024-10-10 17.58.45.png

完了したらターミナルからサーバ起動コマンド( bin/dev )を実行します。
このターミナルはコンテナ内に接続されているのでローカルにruby環境がなくてももちろん実行できます。

サーバー起動後にlocalhost:3000にアクセスすれば普通にrailsアプリケーションの初期画面を見ることができます。

スクリーンショット 2024-10-10 18.04.01.png

これにより、ローカルの開発環境をクリーンに保ちながら、プロジェクト開発を迅速に開始できます。

3
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
3
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?