LoginSignup
60
67

More than 5 years have passed since last update.

link_toで画像とテキストを使ったリンクボタンを作りたい

Last updated at Posted at 2017-10-19

やりたいこと

こんな感じの画像+テキストなボタンを作りたい。
スクリーンショット 2017-10-19 17.49.14.png

<a href="https://sample.com/products/advanced_search" target="_blank">
  <img src="images/search.png">
  高度な検索
</a>

やり方

link_to ... do ... endを使う。

ERBの場合

<%= link_to products_advanced_search_path, target: "_blank" do %>
  <%= image_tag "search.png" %>
  高度な検索
<% end %>

Slimの場合

= link_to products_advanced_search_path, target: "_blank" do
  = image_tag "search.png"
  | 高度な検索

追記:
@scivola さんからコメントでご指摘を頂いた通り、上記のケースではブロックパラメーターがないので、doは省略できます。

= link_to products_advanced_search_path, target: "_blank"
  = image_tag "search.png"
  | 高度な検索

ご指摘ありがとうございます!

60
67
4

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
60
67