この記事を参考に、「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
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2017/10/10 01:54