LoginSignup
14

More than 3 years have passed since last update.

Rails link_toメソッドについて

Last updated at Posted at 2019-07-20

link_toメソッドとは

引数を指定することで、リンクを生成してくれるメソッド
Railsではビューファイルで使用する。

ビューファイルのRubyタグ内で宣言することで
HTMLのaタグの様に使用することができる。

以下メソッド構造

<%= link_to 'リンクの文言', 'パス', class: 'クラス名', method: :HTTPリクエスト %>

methodを定義することで、リンクを踏んだ際のルーティング
を指定することができる。

以下使用例

<%= link_to '一覧へ', '/tweets', class: 'class' method: :get %>

上記の様に記述することで、HTMLのaタグと同じ扱いになる。
以下記述例

<a class="class" href="/tweets">一覧へ</a>

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
14