0
1

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 3 years have passed since last update.

Rails のlink_to メソッドにHTML タグを含める

Posted at

はじめに

Bootstrap を使用したフロントの構築に必要だったので備忘録です

環境

  • macOS 10.15.6
  • Ruby 2.5.7
  • Rails 5.2.3

参照

link_to (ActionView::Helpers::UrlHelper) - APIdock

目標

link_to メソッドを使ったリンクの生成にHTML タグを含めたい

# 目標とするHTML
<a href="path_to_login"><span>Login</span></a>

解決策

link_to のブロック構文を使ってHTML を内包する

# 一般的なlink_to の使い方
<%= link_to "Link Text", path %>

# ブロック構文を使ってHTML を含ませる
<%= link_to path do %>
  含めたいHTML
<% end %>

目標とするHTML のように<a> タグの中に<span> タグを含める場合は下記のように記述する

<%= link_to login_path do %>
  <span>Login</span>
<% end %>

学び

フロントは全然触っていないので慣れないですが色々とやってみるしかないですね...!

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?