LoginSignup
0
0

More than 1 year has passed since last update.

link_toメソッドを使用して各記事へのリンクを作成する。

Posted at

link toメソッドを使用して、パスを指定する際の方法。

①rails routesでパスを確認する

Verb   URI Pattern  Controller#Action
GET    /prototypes/:id(.:format)  prototypes#show

省略していますが、今回はprototypeという記事の詳細ページへのリンクです。
記事はviewである分だけ表示するよう指定しています。

pathはPrefixで記述した方が良いらしいのですが、今回のprototypes#showにはPrefixがありませんので、URI Patternで記述します。

②実際の記述

<%= link_to "#{prototype.title}", "/prototypes/#{prototype.id}", class: :card__title%>

"/prototypes/#{prototype.id}"の部分がパスですね。
実際のURLもこのようになっています。
http://localhost:3000/prototypes/2
 #{}で記述することで、取得した情報を文字列で表示してくれる。
※URLは文字列型

簡単ですが、link toメソッドを使用して各記事へのリンクの作成方法でした。

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