2024-06-15

久々に rails new するぞ

devcontainer で rails new してみるのがゴール

ディレクトリを作っていく

$ mkdir Rails
$ mkdir rails-playground
$ cd $_
$ ruby -v
$ rbenv local 3.4.0-preview1
$ git init
$ git st
$ git add -A
$ gc

v7.2.0.beta2 を使ってみる

 ❯ bundle init
Writing new Gemfile to /Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/Gemfile
$ vi Gemfile
# frozen_string_literal: true

source "https://rubygems.org"

gem "rails", github: "rails/rails", tag: "v7.2.0.beta2"
$ bundle config set path 'vendor/bundle'
$ bundle install
# すでにディレクトリは作ってあるので rails new . で良いのかあ
# この時点ではまだ bundle exec
$ bundle exec rails -h
$ bundle exec rails new . -a propshaft -c tailwind -d postgresql

なんか .gitignore とGemfileが衝突しちゃったな。とりあえず Y (overwrite)

pin とか言っている。あとは foreman も入ってくれるのか。便利

       rails  importmap:install
/Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30: warning: /Users/noriyo_tcp/.rbenv/versions/3.4.0-preview1/lib/ruby/3.4.0+0/ostruct.rb was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0. Add ostruct to your Gemfile or gemspec.
/Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30: warning: /Users/noriyo_tcp/.rbenv/versions/3.4.0-preview1/lib/ruby/3.4.0+0/ostruct.rb was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0. Add ostruct to your Gemfile or gemspec.
       apply  /Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/importmap-rails-2.0.1/lib/install/install.rb
  Add Importmap include tags in application layout
      insert    app/views/layouts/application.html.erb
  Create application.js module as entrypoint
      create    app/javascript/application.js
  Use vendor/javascript for downloaded pins
      create    vendor/javascript
      create    vendor/javascript/.keep
  Configure importmap paths in config/importmap.rb
      create    config/importmap.rb
  Copying binstub
      create    bin/importmap
         run  bundle install --quiet
       rails  turbo:install stimulus:install
/Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30: warning: /Users/noriyo_tcp/.rbenv/versions/3.4.0-preview1/lib/ruby/3.4.0+0/ostruct.rb was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0. Add ostruct to your Gemfile or gemspec.
/Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30: warning: /Users/noriyo_tcp/.rbenv/versions/3.4.0-preview1/lib/ruby/3.4.0+0/ostruct.rb was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0. Add ostruct to your Gemfile or gemspec.
       apply  /Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/turbo-rails-2.0.5/lib/install/turbo_with_importmap.rb
  Import Turbo
      append    app/javascript/application.js
  Pin Turbo
      append    config/importmap.rb
         run  bundle install --quiet
/Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30: warning: /Users/noriyo_tcp/.rbenv/versions/3.4.0-preview1/lib/ruby/3.4.0+0/ostruct.rb was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0. Add ostruct to your Gemfile or gemspec.
       apply  /Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/turbo-rails-2.0.5/lib/install/turbo_needs_redis.rb
  Enable redis in bundle
        gsub    Gemfile
         run    bundle install --quiet
  Switch development cable to use redis
        gsub    config/cable.yml
         run  bundle install --quiet
/Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30: warning: /Users/noriyo_tcp/.rbenv/versions/3.4.0-preview1/lib/ruby/3.4.0+0/ostruct.rb was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0. Add ostruct to your Gemfile or gemspec.
       apply  /Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/stimulus-rails-1.3.3/lib/install/stimulus_with_importmap.rb
  Create controllers directory
      create    app/javascript/controllers
      create    app/javascript/controllers/index.js
      create    app/javascript/controllers/application.js
      create    app/javascript/controllers/hello_controller.js
  Import Stimulus controllers
      append    app/javascript/application.js
  Pin Stimulus
  Appending: pin "@hotwired/stimulus", to: "stimulus.min.js"
      append    config/importmap.rb
  Appending: pin "@hotwired/stimulus-loading", to: "stimulus-loading.js"
      append    config/importmap.rb
  Pin all controllers
  Appending: pin_all_from "app/javascript/controllers", under: "controllers"
      append    config/importmap.rb
         run  bundle install --quiet
       rails  tailwindcss:install
/Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30: warning: /Users/noriyo_tcp/.rbenv/versions/3.4.0-preview1/lib/ruby/3.4.0+0/ostruct.rb was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0. Add ostruct to your Gemfile or gemspec.
/Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30: warning: /Users/noriyo_tcp/.rbenv/versions/3.4.0-preview1/lib/ruby/3.4.0+0/ostruct.rb was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0. Add ostruct to your Gemfile or gemspec.
       apply  /Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/tailwindcss-rails-2.6.1-x86_64-darwin/lib/install/tailwindcss.rb
  Add Tailwindcss include tags and container element in application layout
      insert    app/views/layouts/application.html.erb
      insert    app/views/layouts/application.html.erb
      insert    app/views/layouts/application.html.erb
  Build into app/assets/builds
      create    app/assets/builds
      create    app/assets/builds/.keep
      append    .gitignore
  Add default config/tailwindcss.config.js
      create    config/tailwind.config.js
  Add default app/assets/stylesheets/application.tailwind.css
      create    app/assets/stylesheets/application.tailwind.css
  Add default Procfile.dev
      create    Procfile.dev
  Ensure foreman is installed
         run    gem install foreman from "."
Fetching foreman-0.88.1.gem
Successfully installed foreman-0.88.1
1 gem installed
  Add bin/dev to start foreman
      create    bin/dev
  Compile initial Tailwind build
         run    rails tailwindcss:build from "."
/Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30: warning: /Users/noriyo_tcp/.rbenv/versions/3.4.0-preview1/lib/ruby/3.4.0+0/ostruct.rb was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0. Add ostruct to your Gemfile or gemspec.

.devcontainer/Dockerfile はこんな感じ

# Make sure RUBY_VERSION matches the Ruby version in .ruby-version
ARG RUBY_VERSION=3.4.0.preview1
FROM ghcr.io/rails/devcontainer/images/ruby:$RUBY_VERSION
 ❯ bin/rails -h
Usage:
  bin/rails COMMAND [options]

You must specify a command. The most common commands are:

  generate     Generate new code (short-cut alias: "g")
  console      Start the Rails console (short-cut alias: "c")
  server       Start the Rails server (short-cut alias: "s")
  test         Run tests except system tests (short-cut alias: "t")
  test:system  Run system tests
  dbconsole    Start a console for the database specified in config/database.yml
               (short-cut alias: "db")
  plugin new   Create a new Rails railtie or engine

All commands can be run with -h (or --help) for more information.

In addition to those commands, there are:

/Users/noriyo_tcp/MyPlayground/til/Rails/rails-playground/vendor/bundle/ruby/3.4.0+0/gems/bootsnap-1.18.3/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30: warning: /Users/noriyo_tcp/.rbenv/versions/3.4.0-preview1/lib/ruby/3.4.0+0/ostruct.rb was loaded from the standard library, but will no longer be part of the default gems since Ruby 3.5.0. Add ostruct to your Gemfile or gemspec.

ostruct が 3.5.0 でデフォルトではなくなるので Gemfile に追加するようにとの警告が出ている。とりあえず Gemfile に追加してインストール

devcontainer で Docker のパスが見つからない

https://github.com/microsoft/vscode-remote-release/issues/9105

VS Code settings Dev > Containers: Docker Path でパスを指定しておく

# これで表示されるパスを指定しておく
 ❯ which docker
 /Users/noriyo_tcp/.docker/bin/docker

ruby 3.4.0-preview1 を使っていたせいで devcontainer がこけてしまった

3.3.3 をインストールする

 ❯ rbenv install 3.3.3
 ❯ bundle install --redownload
Fetching gem metadata from https://rubygems.org/.........
Your bundle is locked to strscan (3.1.1) from rubygems repository https://rubygems.org/ or installed locally, but that version can no longer be
found in that source. That means the author of strscan (3.1.1) has removed it. You'll need to update your bundle to a version other than strscan
(3.1.1) that hasn't been removed in order to install.

# Gemfile を削除して bundle install --redownload 再度実行
rm Gemfile.lock

.devcontainer/devcontainer.json にて

なんかコンテナ立ち上げた後の bin/setup はあるが、コンテナ内で bundle install するような記述はない
ということは rails はローカルに入れておくということなのかなやっぱり

もう一度 devcontainer を開こうとするとこんなエラーが

Error response from daemon: No such image: ghcr.io/rails/devcontainer/images/ruby:3.4.0.preview1

image の指定を修正する

.devcontainer/Dockerfile

ここでまだ 3.4.0-preview1 のほうを見ていたので修正した root の Dockerfile も

--- a/.devcontainer/Dockerfile
+++ b/.devcontainer/Dockerfile
@@ -1,3 +1,3 @@
 # Make sure RUBY_VERSION matches the Ruby version in .ruby-version
-ARG RUBY_VERSION=3.4.0.preview1
+ARG RUBY_VERSION=3.3.3

diff --git a/Dockerfile b/Dockerfile
index d06233a..07b3931 100644
--- a/Dockerfile
+++ b/Dockerfile
@@ -7,7 +7,7 @@
 # For a containerized dev environment, see Dev Containers: https://guides.rubyonrails.org/getting_started_with_devcontainer.html

 # Make sure RUBY_VERSION matches the Ruby version in .ruby-version
-ARG RUBY_VERSION=3.4.0.preview1
+ARG RUBY_VERSION=3.3.3
 FROM docker.io/library/ruby:$RUBY_VERSION-slim as base

ここまでやって Rebuild and reopen in container

    volumes:
      - type: bind
        source: /Users/noriyo_tcp/MyPlayground/til/Rails
        target: /workspaces
        bind:
          create_host_path: true

見てると redis, standalone-chromium, postgres などのイメージを構築している

コンテナの中でわざわざまた bundle install しているぞ?

# in container
bin/dev

立ち上がった! localhost:3000 で確認できた

# on Host
 ❯ docker ps -a
CONTAINER ID   IMAGE                           COMMAND                   CREATED         STATUS         PORTS                      NAMES
eec507746391   rails_playground-rails-app      "/bin/sh -c 'echo Co…"   7 minutes ago   Up 7 minutes   0.0.0.0:45678->45678/tcp   rails_playground-rails-app-1
5e8a9016951e   postgres:16.1                   "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes   5432/tcp                   rails_playground-postgres-1
1ef1dd24896a   seleniarm/standalone-chromium   "/opt/bin/entry_poin…"   7 minutes ago   Up 7 minutes   4444/tcp, 5900/tcp         rails_playground-selenium-1
71ee2fc88442   redis:7.2                       "docker-entrypoint.s…"   7 minutes ago   Up 7 minutes   6379/tcp                   rails_playground-redis-1

一旦 Docker Desktop でサクッと止めておいた

コンテナの中だけにしたい

見た感じコンテナ内で bundle install しているようだから、ホスト側に入れておく必要はないのでは?
Remote connection とコンテナ止めておく

# on Host
gem list --no-versions | grep "^[a-z]" | xargs gem uninstall -aIx
rbenv exec gem list #=> rails はない
❯ rails -h
rbenv: rails: command not found

The `rails' command exists in these Ruby versions:
  2.1.1
  2.2.3
  2.5.3
  2.7.2
  2.7.3
  3.0.0
  3.1.2
  3.2.0
  # 3.3.3 には rails はない

そしてまた reopen in container する。コンテナ内ではすでに rails やその他 gem を入れてあるのですんなり動く

container で使用する拡張機能を指定する

該当する extension をタブで開いてadd devcontainers.json を押せば良い

Remote 用の設定画面で Solargraph: Use Bundler をオンにしてみた


Rals Guides の Getting Started をやっていくぞ

link_to の第2引数はパスヘルパーを使用しなくても、リソースを渡せば良いのだな?
元々そうだったっけ? https://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

そうだったっぽい

<%= link_to article.title, article %>

Deleting and Article

https://guides.rubyonrails.org/getting_started.html#deleting-an-article

今では data-turbo-method, data-turbo-confirm を使うのだなあ

あとは Basic authentication を残すのみ

status を enum にして inclusion validation 使いたいな