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

Ruby on ReilsでルートのPrefix作成する方法およびDBデータのリンクを作る方法

Last updated at Posted at 2019-09-18

現状

作成中のサイト

食べログみたいな飲食店情報サイトのポータルサイト

都道府県一覧ページ

show.rb
<h1><%= @prefectures.prefname %>のお店一覧</h1>

<% @stores.each do |store| %>
    <% if store.prefecture == @prefectures.prefname %>
        <h3><%=store.name%></h3>
        <p><%=store.smoking%></p>
        <p><%=store.city%></p>
    <% end %>
<% end %>

上記で、表示させているのは各都道府県のページに住所がその都道府県に該当する店舗データを表示させています。

Prefix作成方法

Prefixの確認


$ rails routes
                   Prefix Verb   URI Pattern 
                     root GET    /static#home
                    users GET    /users(.:format)          users#index
                          POST   /users(.:format)          users#create
                 new_user GET    /users/new(.:format)      users#new
                edit_user GET    /users/:id/edit(.:format) users#edit
                     user GET    /users/:id(.:format)      users#show
                          PATCH  /users/:id(.:format)      pref GET    
                          GET    /detail/:id(.:format)     store#detail

link_toメソッドでリンク先を作成しますが普段通りにrails routesを行ってもPrefixfaが設定されていないので別途設定を行います。
リンクを作成するのはGET /detail/:id(.:format) store#detailになります。

routes.rbの設定

routes.rb
Rails.application.routes.draw do

  root 'static#home'
(省略)
  get '/detail/z_:id',   to: 'store#detail'

蒸気が現状のコードになります。
そこから下記の様に**as:**をつけることでPrefixを設定することができます。

routes.rb
Rails.application.routes.draw do

  root 'static#home'
(省略)
  #asでPrefix の設定ができる
  get '/detail/z_:id',   to: 'store#detail', as: 'detail'

再度確認


$ rails routes
                   Prefix Verb   URI Pattern 
                     root GET    /static#home
                    users GET    /users(.:format)          users#index
                          POST   /users(.:format)          users#create
                 new_user GET    /users/new(.:format)      users#new
                edit_user GET    /users/:id/edit(.:format) users#edit
                     user GET    /users/:id(.:format)      users#show
                          PATCH  /users/:id(.:format)      pref GET    
                   detail GET    /detail/:id(.:format)     store#detail

これでPrefix作成は完了です。

DBデータのリンク作成方法

Prefix作成をしたのちにlink_toメソッドをしようして普段通りにpathを記載し

<h3><%=link_to store.name , detail_path %> </h3>

pathの後ろにidが入る様に設定すれば完了(↓のコードで完成)

<h3><%=link_to store.name , detail_path(store.id) %> </h3>

全体図

show.rb
<h1><%= @prefectures.prefname %>のお店一覧</h1>

<% @stores.each do |store| %>
    <% if store.prefecture == @prefectures.prefname %>
        <h3><%=link_to store.name , detail_path(store.id) %> </h3>
        <p><%=store.smoking%></p>
        <p><%=store.city%></p>
    <% end %>
<% 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?