ユーザーがネストされた質問と回答に投票できるようにするレールアプリを構築しています。
このコードは、ユーザーが回答に投票できるようにすることを意図したとおりに機能します。
<%= link_to raw('<i class="icon-2x icon-chevron-up"></i>'), vote_question_answer_path(@question, answer, :type => :up), :method => :post %>
しかし、このコードは ajax 経由で同じことを行う必要があります。
<%= link_to raw('<i class="icon-2x icon-chevron-up"></i>'), vote_question_answer_path(@question, answer, :type => :up), :remote => true, :method => :post %>
投票は正しく記録されますが、次のエラーが発生します。
ActionController::RoutingError at /questions/fugiat-nulla-blue-bottle-raw-denim-fap-sint-butcher-ethical-cosby-sweater-thundercats-distillery-laboris-tofu/answers/6/vote=========================================================================================================================================================================> No route matches {:action=>"vote", :controller=>"answers", :type=>:up, :question_id=>nil, :id=>#<Answer id: 6, body: "Ullamco Pinterest food truck incididunt trust fund,...", user_id: 1, question_id: 10, created_at: "2013-09-10 22:20:40", updated_at: "2013-09-10 22:20:40">}app/views/shared/_vote_answer_arrows.html.erb, line 6
私は混乱しています-2つは同じルートに投稿されていませんか? 1 つは機能し、もう 1 つはルーティング エラーになるのはなぜですか?
問題はネストされたルートに関連していると思います。これは、質問のコードがほぼ同じであり、AJAX ルートがそこで機能するためです。
編集:要求に応じて、関連するルートは次のとおりです。
resources :questions do
member { post :vote }
resources :answers do
member { post :vote }
end
end
そして、respond_to do |format|
respond_to do |format|
format.html{ redirect_to :back, :flash => { :success => 'Thank you for voting!' }}
format.js {}
end
firebug からの AJAX エラー メッセージ/URL は次のとおりです。
"NetworkError: 500 Internal Server Error - http://localhost:3000/questions/fugiat-nulla-blue-bottle-raw-denim-fap-sint-butcher-ethical-cosby-sweater-thundercats-distillery-laboris-tofu/answers/5/vote?type=down"
そして、これがfirebugのAJAXパラメーターです(これは私には非常に間違っているように見えるので、それが問題の原因なのか、それとも私がfirebugに慣れていないだけなのかわかりません:
type down
そして、サーバー出力からの成功した非 AJAX 要求のパラメーターを次に示します。
Parameters: {"authenticity_token"=>"X3Dg3TSZ8blMNTT1Zce2R0A1rtZI93ViFJNsjOP145w=", "type"=>"up", "question_id"=>"fugiat-nulla-blue-bottle-raw-denim-fap-sint-butcher-ethical-cosby-sweater-thundercats-distillery-laboris-tofu", "id"=>"3"}