LoginSignup
22
13

More than 3 years have passed since last update.

Rails link_toにパラメータを渡す

Posted at

link_toを使ってgetsでコントローラにパラメータを受け渡したい

やりたいこと

view側からコントローラにリンクする際に、値を受け渡したい

結論

以下のようにパスの引数にハッシュの形で追加してやる

※例)top/indexからtop/link1へ値を渡す場合

top/index.html.erb
  <%= link_to "リンク1へ", top_link1_path(name: "piyo") %>

パラメータを受け取る

top_controller
class TopController < ApplicationController
  def link1
    @params = params[:name]
  end
end

色々と調べてみましたが、備忘録として

22
13
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
22
13