ネストされたリソースを試しています:
私のルート:
resources :conversations do
resources :replies do
resources :comments
end
end
会話で機能する返信用のフォームを取得できましたが、返信を処理するためにコメントを取得するという複雑さを追加しています。
フォーム全体はすべて会話ショー パスの下にあります。
<%= form_for([@conversation, @reply]) do |f| %>
<%= render 'shared/response_form', f: f %>
<%= f.submit "Reply", class: "btn btn-large btn-primary" %>
<% end %>
返信用の上記のフォームは正常に機能し、エラーは発生しません。コメント用の以下のフォームはエラーを受け取ります。
未定義のメソッド「reply_comments_path」
<%= form_for([@reply, @comment]) do |f| %>
<%= render 'shared/response_form', f: f %>
<%= f.submit "Comment", class: "btn btn-large btn-primary" %>
<% end %>
これがショーの会話コントローラーです。これが問題だと思うところです。
def show
@conversation = Conversation.find(params[:id])
@replies = @conversation.replies
@reply = current_user.replies.build
#If I change the above line to @conversations.replies.build
#it breaks the ability to show replies above the form.
@comments = @reply.comments
@comment = @reply.comments.build
end
ただし、他の誰かがこれを行うことを提案しました:
<%= form_for([@conversation, @reply, @comment]) do |f| %>
<%= render 'shared/response_form', f: f %>
<%= f.submit "Comment", class: "btn btn-large btn-primary" %>
<% end %>
しかし、それはルーティングエラーで終わっただけです:
No route matches {:controller=>"comments", :format=>nil, :conversation_id=>#<Conversation id: 3, content: "Goes here.", user_id: 1, created_at: "2012-12-10 21:20:01", updated_at: "2012-12-10 21:20:01", subject: "Another conversation">, :reply_id=>#<Reply id: nil, content: nil, user_id: 1, created_at: nil, updated_at: nil, conversation_id: nil>}
新しいフォームを作成しようとすると、常にこの未定義のメソッドパスエラーが発生し、何が間違っていたかを常に忘れてしまいます。答えは決してルートではないようです。
編集:
コントローラーの作成セクションの下に、次のものがあります。
@replies = @conversation.replies
@reply = current_user.replies.build
#If I change the above line to @conversations.replies.build
#it breaks the ability to show replies above the form.
@reply = @conversation.replies.build が既存の返信を表示する機能を壊す理由がわかりません。nil を数値に変換できないというエラーが表示され、reply.created_at または reply.content が表示されません。原因が何であれ、私がこの問題を抱えている理由の手がかりになるかもしれません。ただし、私が使用している返信コントローラーでは
@reply = conversation.replies.build(content: params[:reply][:content], user_id: current_user.id)
編集:
付け加えると、Stackoverflow は、私がここで達成しようとしていることと非常によく似た処理を行います。ただし、回答だけでなく質問にもコメントできる点が異なります。