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

More than 1 year has passed since last update.

【Rails7】link_to Prefix method: :postが効かなかった問題を解決

Posted at

バージョン

ruby 3.0.2
rails 7.0.4

前提

スクリーンショット 2023-02-21 1.52.31.png
prefixを使って画面遷移する際に、getメソッドを呼ぶことはできたがpostメソッドを呼ぶことができなかった。

GETメソッドを呼びたい場合はこれ

erb
<%= link_to item_cart_items_path(@item.id) %>

問題のコード

erb
<%= link_to item_cart_items_path(@item.id), method: :post %>

これでPOSTリクエストが送信されているかと思ったら送信できていなかった。

解決

erb
<%= link_to item_cart_items_path(@item.id), data: { turbo_method: :post } %>

rails7からmethod: :postの書き方はできなくなったみたいです。
参考

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