0
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

link_toの書き方パターンのまとめ

0
Last updated at Posted at 2019-10-14

はじめに

RailsでECサイトをつくっているときに、 link_toの書き方でつまづいたので、いくつかのパターンをまとめてみました。

環境

・MacOS High Sierra バージョン10.13.4
・Rails 5.2.1

link_toのいろいろな書き方

◆urlを使う


<%= link_to “Qiita”, “https://qiita.com” %>

◆パスを使う


<%= link_to “Tシャツ”, product_path %>

◆クラスを指定する


<%= link_to “Tシャツ”, product_path , class: “btn viewBtn” %>

◆囲う


<%= link_to product_path(product.id) do %>  <h2><%= product.name %></h2>  <h3><%= product.display_price %></h3> <% end %>

おわりに

link_toは、けっこう使う頻度が高かったので、いろんな書き方のパターンについてまとめてみましたが、まだまだ覚えることがたくさんありそうです!

参考

[ActionView::Helpers::UrlHelper - Rails API - Ruby on Rails]
(https://api.rubyonrails.org/v5.2.3/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to)

0
4
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
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?