LoginSignup
8
9

More than 5 years have passed since last update.

link_toでPOST指定する時にパラメーターを渡す方法

Posted at

link_toで値を渡して、methodも指定したい時

メソッドを指定してviewからcontrollerに値を渡すときに

<%= link_to "hoge", controller "users", action "update", user_id: 1, method: :post %>

このコードだとmethod: :postを上手く読み取ってもらえず、なぜかリクエストがGETになってしまった。

そのため

<%= link_to "hoge", {controller: "users", action: "update", user_id: 1}, method: :post %>

このようにメソッドだけ括弧で分けてあげると無事に動作しました。
分けてあげないとmethod: :postもパラメーターと勘違いされちゃうんですね

名前付きルートの場合の記述の仕方がまだ分かっていないため、どなたかコメントで教えて頂けたら嬉しいです。

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