0
0

More than 3 years have passed since last update.

Rails link_toメソッドについて

Posted at

link_toメソッドとは

引数を指定してあげることでリンクを作成してくれるメソッドです。
railsにおいてビューファイルで使用することができる。
タイトル.html.erbファイルにて宣言してあげることで宣言して使う。
htmlでいうaタグのような役割を持っている

以下のように記述する。

ruby.html.erb
<%= link_to "リンク名”, リンク先のパス, class: "クラス名", method: :HTTPリクエスト %>

メソッドを指定することでリンク先のルーティングを設定できる。
deviseの場合はログアウトするリンクを作りたい時に destroy_user_session_path method: :delete という風に記述する。

以下link_toを使用した記述例

ruby.html.erb
<%= link_to "トップページへ戻る", root_path, class: "class" %>

htmlのaタグを使用しての記述の場合

tital.html
<a class="class名" href="/hogehoge">タイトル</a>

以上です

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