LoginSignup
2
0

More than 1 year has passed since last update.

ええ!?Getting Started with Rails(7)で詰まるなんてあっていいんですか!?

Last updated at Posted at 2022-03-10

-1: これは何?

公式チュートリアルGetting Started with Rails v7.0.2.3を進める上で詰まったこと備忘録(進行形).

0: 環境について

追記:rails7-on-dockergit cloneするのがとても良いと思います.

1: Failed to save 'index.html.erb'うんたらかんたら

詳細

比較的詳細なエラーコードは以下:

Failed to save 'index.html.erb': 
Command failed: 
"C:\Users\なんやかんや\Microsoft VS Code\bin\code.cmd"--file-write 
"c:\Users\ユーザー名\AppData\Roaming\Code\code-elevated-QSbagCFM" 
"\\wsl$\Ubuntu\home\なんやかんや\docker\rubytest\app\views\articles\index.html.erb"
Error using --file-write: EPERM: operation not permitted,
open '\\wsl$\Ubuntu\home\なんやかんや\docker\rubytest\app\views\articles\index.html.erb'

(改行,ユーザー名置換は筆者による)

解決

これはコンテナを再起動したら通るようになった.SQL側のコンテナとの疎通がうまくいってないのだろうか?情報モトム.

2: Unable to write file (NoPermissions (FileSystemError): Error: EPERM: operation not permitted

VSCodeで編集したファイルを保存しようとしたときに発生.

解決

とにかく権限がないとのことなので個別に

sudo chown -R $USER:$USER .

するなどしてひとまずの解決を見た.
原因についてだが,bin/railsコマンドで生成されたファイルについて↑のエラーが出ていることから,コンテナ内がrootで走っていることによってファイルの所有権がrootになっているのが起因しているのかもしれない.
毎回chownするのも億劫.設定ファイルを作ろうか,考え中である.
参考までに,WSL2とDockerの権限に関しては次のサイトが簡潔にまとまっているように思う:【Docker】 WSL 2 を利用したコンテナー内開発で権限をどう設定するべきか

3: data-turbo-method="delete" returns GET request 問題

Getting Started with Rails 7.5 Deleting an Articleにおいて

app/views/show.html.erb
  <li><%= link_to "Destroy", article_path(@article), data: {
                    turbo_method: :delete,
                    turbo_confirm: "Are you sure?"
                  } %></li>

が正常に動作しない,すなわち本来DELETE requestを送るはずがGET requestが送られてしまう状態に陥った.

詳細

本来は下記のようなログになるはずが(DELETE 303 See Otherが期待される),

web_1  | Started DELETE "/articles/2" for 172.21.0.1 at 2022-03-10 04:33:05 +0000
web_1  | Cannot render console from 172.21.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
web_1  | Processing by ArticlesController#destroy as HTML
web_1  |   Parameters: {"authenticity_token"=>"[FILTERED]", "id"=>"2"}
web_1  |   Article Load (0.5ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" = $1 LIMIT $2  [["id", 2], ["LIMIT", 1]]
web_1  |   ↳ app/controllers/articles_controller.rb:40:in `destroy'
web_1  |   TRANSACTION (0.4ms)  BEGIN
web_1  |   ↳ app/controllers/articles_controller.rb:41:in `destroy'
web_1  |   Article Destroy (0.5ms)  DELETE FROM "articles" WHERE "articles"."id" = $1  [["id", 2]]
web_1  |   ↳ app/controllers/articles_controller.rb:41:in `destroy'
web_1  |   TRANSACTION (3.2ms)  COMMIT
web_1  |   ↳ app/controllers/articles_controller.rb:41:in `destroy'
web_1  | Redirected to http://localhost:3000/
web_1  | Completed 303 See Other in 11ms (ActiveRecord: 4.5ms | Allocations: 2667)

次のようになってしまっていた(GET 200 OKとなっている).

web_1  | Started GET "/articles/1" for 172.21.0.1 at 2022-03-10 04:33:07 +0000
web_1  | Cannot render console from 172.21.0.1! Allowed networks: 127.0.0.0/127.255.255.255, ::1
web_1  | Processing by ArticlesController#show as HTML
web_1  |   Parameters: {"id"=>"1"}
web_1  |   Article Load (0.5ms)  SELECT "articles".* FROM "articles" WHERE "articles"."id" = $1 LIMIT $2  [["id", 1], ["LIMIT", 1]]
web_1  |   ↳ app/controllers/articles_controller.rb:7:in `show'
web_1  |   Rendering layout layouts/application.html.erb
web_1  |   Rendering articles/show.html.erb within layouts/application
web_1  |   Rendered articles/show.html.erb within layouts/application (Duration: 1.0ms | Allocations: 456)
web_1  |   Rendered layout layouts/application.html.erb (Duration: 5.0ms | Allocations: 2690)
web_1  | Completed 200 OK in 8ms (Views: 5.5ms | ActiveRecord: 0.5ms | Allocations: 3447)

解決(?)

ひとまず該当箇所を

  <li><%= button_to "Destroy", article_path(@article),
                  method: :delete,
                  data: { turbo_confirm: "Are you sure?" } %></li>

と修正した.この解決はそのままcan't delete single article with rails sample project, which destroy will still make GET method in rails 7.0によるものである.
これでひとまずDELETEリクエストを送れてはいるようだ.ただ,turbo_confirmは依然として機能していないのは変わらず...
回答者によると
Rails 7 is using hotwire and turbo instead of Rails/ujs. So link_to have some problems, change link_to to button_to, it should work.とのこと.ウッソーマジで?といった感じである.

また,上記リンク先では下記ツイートについて触れられていたのでリンクを貼っておく.

一方で,疑問点もいくつかある.

  • 公式サイト含め,他のサイト等では(turbo_)?method: deleteをdataに渡すとしている(この辺は仕様を含め要検証)
  • 結局(turbo_)?confirmでもalermが出ない(これはDaniel Chen氏の言う通り)
  • ということはHotwire,Turbo関連が上手く動作していないのでは?gem listではちゃんとインストールできてるみたいなんですが..
  • そもそも公式ガイドで難点をつくらないでください(自分が特殊なバグに当たってるだけ)
  • とりあえずRailsのドキュメントや実装を見たりする予定
  • 上手くいったり行かなかったりするケースがあるようなので,今後は双方(rails7-on-dockerとの)の設定ファイルやログを見比べてみる.

結論

いまだよくわかってないです.進展があれば追記予定.

2
0
6

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