我正在构建一个 Rails 应用程序,让用户对嵌套的问题和答案进行投票:
此代码按预期工作,让用户投票赞成答案:
<%= 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
我很困惑 - 这两个帖子不是在同一条路线上吗?为什么一个工作而另一个给出路由错误?
我相信这个问题与嵌套路由有关,因为我有几乎相同的问题代码,并且 AJAX 路由在那里工作。
编辑:根据要求,以下是相关路线:
resources :questions do
member { post :vote }
resources :answers do
member { post :vote }
end
end
而respond_to 做|格式|
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"}