この記事を参考に、「5. 各ユーザーごとの投稿したブログ記事一覧が見れる」をやってみたのですが、
各ユーザーのコメント一覧から、そのユーザーがコメントした記事に戻りたいのですが、ルーティングの指定方法がわからず困っています。
『あるユーザーが投稿した記事に対してコメントした、その自分のコメント一覧を取得し、そこから、元のコメントした記事に飛ぶ』という感じです。
(この場合は記事ではなく、songに対してコメント、という風になっている)
おわかりであれば、回答よろしくお願いします。
index.html.erb↓
<% if @user.present? %>
<h1><%= @user.name %>さんの記事一覧</h1>
<% else %>
<h1>全ユーザーの投稿一覧</h1>
<% end %>
<% @comments.each do |comment| %>
<li>タイトル: <%= link_to comment.body, #ここが何にしたらいいのかわかりません# %></li>
<% end %>
routes.rb↓
resources :comments
devise_for :users
resources :users, :only => [:index, :show] do
resources :comments
end
resources :songs do
member do
put "like", to: "songs#upvote"
end
resources :comments do
member do
put "good", to: "comments#upvote"
end
end
end
root to: "songs#index"
rake routes↓
comments GET /comments(.:format) comments#index
POST /comments(.:format) comments#create
new_comment GET /comments/new(.:format) comments#new
edit_comment GET /comments/:id/edit(.:format) comments#edit
comment GET /comments/:id(.:format) comments#show
PATCH /comments/:id(.:format) comments#update
PUT /comments/:id(.:format) comments#update
DELETE /comments/:id(.:format) comments#destroy
new_user_session GET /users/sign_in(.:format) devise/sessions#new
user_session POST /users/sign_in(.:format) devise/sessions#create
destroy_user_session DELETE /users/sign_out(.:format) devise/sessions#destroy
user_password POST /users/password(.:format) devise/passwords#create
new_user_password GET /users/password/new(.:format) devise/passwords#new
edit_user_password GET /users/password/edit(.:format) devise/passwords#edit
PATCH /users/password(.:format) devise/passwords#update
PUT /users/password(.:format) devise/passwords#update
cancel_user_registration GET /users/cancel(.:format) devise/registrations#cancel
user_registration POST /users(.:format) devise/registrations#create
new_user_registration GET /users/sign_up(.:format) devise/registrations#new
edit_user_registration GET /users/edit(.:format) devise/registrations#edit
PATCH /users(.:format) devise/registrations#update
PUT /users(.:format) devise/registrations#update
DELETE /users(.:format) devise/registrations#destroy
user_comments GET /users/:user_id/comments(.:format) comments#index
POST /users/:user_id/comments(.:format) comments#create
new_user_comment GET /users/:user_id/comments/new(.:format) comments#new
edit_user_comment GET /users/:user_id/comments/:id/edit(.:format) comments#edit
user_comment GET /users/:user_id/comments/:id(.:format) comments#show
PATCH /users/:user_id/comments/:id(.:format) comments#update
PUT /users/:user_id/comments/:id(.:format) comments#update
DELETE /users/:user_id/comments/:id(.:format) comments#destroy
users GET /users(.:format) users#index
user GET /users/:id(.:format) users#show
like_song PUT /songs/:id/like(.:format) songs#upvote
good_song_comment PUT /songs/:song_id/comments/:id/good(.:format) comments#upvote
song_comments GET /songs/:song_id/comments(.:format) comments#index
POST /songs/:song_id/comments(.:format) comments#create
new_song_comment GET /songs/:song_id/comments/new(.:format) comments#new
edit_song_comment GET /songs/:song_id/comments/:id/edit(.:format) comments#edit
song_comment GET /songs/:song_id/comments/:id(.:format) comments#show
PATCH /songs/:song_id/comments/:id(.:format) comments#update
PUT /songs/:song_id/comments/:id(.:format) comments#update
DELETE /songs/:song_id/comments/:id(.:format) comments#destroy
songs GET /songs(.:format) songs#index
POST /songs(.:format) songs#create
new_song GET /songs/new(.:format) songs#new
edit_song GET /songs/:id/edit(.:format) songs#edit
song GET /songs/:id(.:format) songs#show
PATCH /songs/:id(.:format) songs#update
PUT /songs/:id(.:format) songs#update
DELETE /songs/:id(.:format) songs#destroy
root GET / songs#index
-
気になる質問をクリップする
クリップした質問は、後からいつでもマイページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
クリップを取り消します
-
良い質問の評価を上げる
以下のような質問は評価を上げましょう
- 質問内容が明確
- 自分も答えを知りたい
- 質問者以外のユーザにも役立つ
評価が高い質問は、TOPページの「注目」タブのフィードに表示されやすくなります。
質問の評価を上げたことを取り消します
-
評価を下げられる数の上限に達しました
評価を下げることができません
- 1日5回まで評価を下げられます
- 1日に1ユーザに対して2回まで評価を下げられます
質問の評価を下げる
teratailでは下記のような質問を「具体的に困っていることがない質問」、「サイトポリシーに違反する質問」と定義し、推奨していません。
- プログラミングに関係のない質問
- やってほしいことだけを記載した丸投げの質問
- 問題・課題が含まれていない質問
- 意図的に内容が抹消された質問
- 過去に投稿した質問と同じ内容の質問
- 広告と受け取られるような投稿
評価が下がると、TOPページの「アクティブ」「注目」タブのフィードに表示されにくくなります。
質問の評価を下げたことを取り消します
この機能は開放されていません
評価を下げる条件を満たしてません
質問の評価を下げる機能の利用条件
この機能を利用するためには、以下の事項を行う必要があります。
- 質問回答など一定の行動
-
メールアドレスの認証
メールアドレスの認証
-
質問評価に関するヘルプページの閲覧
質問評価に関するヘルプページの閲覧
checkベストアンサー
0
<%= link_to comment.body, comment.song %>
または
<%= link_to comment.body, song_path(comment.song) %>
CommentモデルにSongへのリレーションが必要です
class Comment < ...
belongs_to :song
end
投稿
-
回答の評価を上げる
以下のような回答は評価を上げましょう
- 正しい回答
- わかりやすい回答
- ためになる回答
評価が高い回答ほどページの上位に表示されます。
-
回答の評価を下げる
下記のような回答は推奨されていません。
- 間違っている回答
- 質問の回答になっていない投稿
- スパムや攻撃的な表現を用いた投稿
評価を下げる際はその理由を明確に伝え、適切な回答に修正してもらいましょう。
15分調べてもわからないことは、teratailで質問しよう!
- ただいまの回答率 88.36%
- 質問をまとめることで、思考を整理して素早く解決
- テンプレート機能で、簡単に質問をまとめられる
2017/10/10 10:54
ルーティングについて勉強しようと思います。。。。。。