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メソッドの指定を省略して記述することができます。