0

ネストされたリソースを使用していますが、リンクを適切な場所に移動させるために渡すオブジェクトがわかりません。コメントは投稿に属し、質問はコメントに属します。投稿のインデックス ページから質問のインデックス ページにリンクしようとしています。

これは、routes.rb ファイルがどのように見えるかです:

resources :posts do
  resources :comments do
 end
end

resources :comments do
  resources :questions do
 end
end

投稿インデックス ファイル内のリンク:

  <% post.comments.select(:body).order('created_at desc').limit(2).each do |comment| %>         

  <%= link_to (comment.body), comment_questions_path(comment, @question) %>
  <% end %>

これにより、次のエラーが表示されます。

missing required keys: [:comment_id]

'rake routes | grep comment_question':

comment_questions GET    /comments/:comment_id/questions(.:format)   questions#index
comment_question GET    /comments/:comment_id/questions/:id(.:format) questions#show                    

ありがとう!

4

2 に答える 2

0

単一のリソースにリンクしているため、正しいパスは次のようになります。

comment_question_path(comment, @question)

それでもうまくいかない場合は、comment_id を明示的に設定してみてください。

comment_question_path(comment.id, @question)

また、rake routes | grep comment_questionコマンドの結果を投稿していただけると助かります。

編集:rake routes出力の 2 番目のエントリが表示されるので、path/url メソッドは 2 つの引数を想定しています。

/comments/:comment_id/questions/:id(.:format)

path メソッドに変換された記号は次のようになります (:id記号は質問 ID です)。

comment_question_path(:comment_id, :id)

エラー メッセージに基づいて、コメントが nil のように見え、URI の次のパス (この場合はquestions) にフォールバックしている可能性があります。

そのパスを使用する前にデバッガー ステートメントをスローして、 と の両方の出力を表示できますpp commentpp @question?

于 2013-10-21T00:44:06.917 に答える