質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

Q&A

解決済

1回答

416閲覧

コメント一覧から、自分のコメントした記事に戻りたい

renren643

総合スコア279

Ruby

Rubyはプログラミング言語のひとつで、オープンソース、オブジェクト指向のプログラミング開発に対応しています。

Ruby on Rails

Ruby on Railsは、オープンソースのWebアプリケーションフレームワークです。「同じことを繰り返さない」というRailsの基本理念のもと、他のフレームワークより少ないコードで簡単に開発できるよう設計されています。

Ruby on Rails 4

Ruby on Rails4はRubyによって書かれたオープンソースのウェブフレームワークです。 Ruby on Railsは「設定より規約」の原則に従っており、効率的に作業を行うために再開発を行う必要をなくしてくれます。

0グッド

0クリップ

投稿2017/10/09 16:34

編集2017/10/09 16:44

この記事を参考に、「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

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

<%= link_to comment.body, comment.song %> または <%= link_to comment.body, song_path(comment.song) %>

CommentモデルにSongへのリレーションが必要です

class Comment < ... belongs_to :song end

投稿2017/10/10 00:28

suzukis

総合スコア1449

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

renren643

2017/10/10 01:54

解決しました!!回答ありがとうございました! ルーティングについて勉強しようと思います。。。。。。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問