4
8

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 5 years have passed since last update.

railsのlink_toでpostする

4
Posted at

遭遇するたび調べているので、自分へのメモも兼ねて、やり方を共有します。

方法

  • methodをpostに指定する
  • authenticity_tokenをクエリに渡す
view.html.erb
<%= link_to(
     'お気に入り',
     favorites_path(user_id: 1, item_id: 3, authenticity_token: form_authenticity_token),
     method: :post
    ) %>

authenticity_tokenを渡さないと、下記のエラーが出ます。

エラーメッセージ
Can't verify CSRF token authenticity

以上です。
問題があったら、指摘してもらえると嬉しいです。

参考

4
8
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
4
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?