LoginSignup
0
0

More than 5 years have passed since last update.

Ruby on Rails - ③ link_toメソッド

Posted at

link_toメソッド

Scaffoldで自動生成されたコード

<td><%= link_to 'Show', user %></td>
<td><%= link_to 'Edit', edit_user_path(user) %></td>
<td><%= link_to 'Destroy', user, method: :delete, data: { confirm: 'Are you sure?' } %></td>

URLやパスを指定

外部のURLにリンクを追加

link_to 'Google', 'http://www.google.co.jp'

「_path」を付けて次のように記述することもできる

routes情報を見て見ましょう〜

$ rails routes

スクリーンショット 2019-04-06 16.01.35.png

_pathの前に「Prefix」をつければいいですね。
editのPrefixは「edit_user」なのでedit_user_pathになります。
editはAction名、userはController名の単数

<td><%= link_to 'Edit', edit_user_path(user) %></td>
0
0
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
0
0