2
0

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.

link_toメソッドを使ったときに、表示させる文字を追加する方法

Posted at

link_toメソッドとは

Railsで、「.html.erb」の拡張子でHTMLでいうところのaタグを生成してくれるメソッド。
Railsで開発を進めるときには、よく使うヘルパーメソッドの一つ。

基本的な書き方


<%= link_to '表示させる文字' , リンク先のURLやpath %>

表示させる文字のところには、テーブルのカラムから呼び出した値を入れることも可能。
ex) post.name@user.nameなど

リンクに表示される文字に呼び出した値を入れ、さらに文字列を追加したいとき


<%= link_to @user.name + "さん", root_path %>

+ "追加したい文字"とする。
「さん」を含めた全体がリンク先に行くマウスカーソルの範囲となる。

2
0
2

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?