8
2

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 1 year has passed since last update.

【Rails】link_toいろいろ

Last updated at Posted at 2020-01-29

アンカーリンク

<%= link_to 'サンプル', sample_path(anchor: 'hogehoge') %>

<%= link_to(sample_path(anchor: 'hogehoge')) do %>
  <p>サンプルテキスト</p>
  <p>Hello World!</p>
<% end %>

パラメーター

<%= link_to 'サンプル', sample_path(id: user.id) %>
<%= link_to(sample_path(text: 'hogehoge')) do %>
  <p>サンプルテキスト</p>
  <p>Hello World!</p>
<% end %>

id付きのパスにパラメーター

<%= link_to 'サンプル', edit_sample_path($user, item: 'hoge') %>

URLを直書き

<%= link_to 'サンプル', 'https://google.com' %>
<%= link_to '/hoge' do %>
  <p>サンプルテキスト</p>
  <p>Hello World!</p>
<% end %>

Formatを指定

<%= link_to '見積書PDFを表示する', quotation_path(@order, format: :pdf) %>

<%= link_to(quotation_path(@order, format: :pdf) do %>
  <span>サンプルテキスト</span>
  <span>見積書PDFを表示する</span>
<% end %>

classやstyle等を記述

<%= link_to 'サンプル', sample_path, { class: 'mb-2', style: 'font-size: 20px;', method: :post} %>

戻るボタン

<%= link_to '戻る', 'javascript:history.back()' %>

参考

Rails でアンカー付きのリンクの書き方
【Rails】戻るボタンの実装方法【複数ページ戻ることも可能】

8
2
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
8
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?