0
0

More than 1 year has passed since last update.

link_toメソッド

Posted at

link_toメソッドとは

リンクを作成するためのヘルパーメソッドです。HTMLのaタグの代わりに使用できます。
ビューファイルでは、以下のように記述します。
タグとして出力するため、<%= %>で囲う必要があります。

リンク先をURLで指定する場合
<%= link_to 'リンクに表示する文字', 'リンク先のURL' %>
[例]
<%= link_to 'Qiita', 'https://qiita.com/' %>
リンク先をパスで指定する場合
<%= link_to 'リンクに表示する文字', 'パス', method: :HTTPメソッド %>
[例]
<%= link_to 'ログイン', new_user_session_path, class: "login" %>
<%= link_to '新規登録', new_user_registration_path, class: "sign-up" %>
<%= link_to '新規投稿', '/posts/new', method: :GET %>

link_toメソッドでパスを指定する場合、何も指定しなければデフォルトでGETメソッドが設定されます。
したがって、GETメソッドの指定を省略して記述することができます。

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