简体   繁体   English

尝试通过AJAX进行发布时出现Rails“无路线匹配”错误

[英]Rails “No route matches” error when trying to POST via AJAX

I am building a rails app that lets users vote on questions and answers, which are nested: 我正在构建一个Rails应用,该应用可以使用户对嵌套的问题和答案进行投票:

This code works as intended to let users vote up an answer: 这段代码旨在让用户对答案进行投票:

<%= link_to raw('<i class="icon-2x icon-chevron-up"></i>'), vote_question_answer_path(@question, answer, :type => :up), :method => :post %>

but this code, which should do the same thing via ajax: 但是这段代码,应该通过ajax做同样的事情:

<%= link_to raw('<i class="icon-2x icon-chevron-up"></i>'), vote_question_answer_path(@question, answer, :type => :up), :remote => true, :method => :post %>

records the vote correctly, but then gives the following error: 正确记录投票,但随后出现以下错误:

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

I'm confused - aren't the two posting to the same route? 我很困惑-这两个发布地点不一样吗? Why is one working and the other giving a routing error? 为什么一个工作正常,而另一个却出现路由错误?

I believe that the problem is related to the nested routes, because I have nearly identical code for the questions, and the AJAX route works there. 我认为问题与嵌套路由有关,因为我对问题的代码几乎相同,并且AJAX路由在那里起作用。

EDIT: As requested, here are the relevant routes: 编辑:根据要求,这是相关的路线:

resources :questions do
  member { post :vote }
  resources :answers do
    member { post :vote }
  end
end

And the respond_to do |format| 和response_do做| format |

respond_to do |format|
  format.html{ redirect_to :back, :flash => { :success => 'Thank you for voting!' }}
  format.js {}
end

Here is the AJAX error message/URL from firebug: 这是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"

And here is AJAX params from firebug (This looks very wrong to me, so I am not sure if it is the source of the problem or just me being unfamiliar with firebug: 这是firebug的AJAX参数(这对我来说似乎很不对劲,所以我不确定这是问题的根源还是只是我不熟悉firebug:

type    down

And here is a successful non-AJAX request's parameters from the server output: 这是服务器输出中成功的非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"}

As I mentioned in the comments above, this turned out to be a simple controller issue. 正如我在上面的评论中提到的,这原来是一个简单的控制器问题。

In my controller, I had not defined @question. 在我的控制器中,我没有定义@question。 This was not a problem until I turned the request into ajax and needed the variable to render the partials in my javascript. 直到我将请求转换为ajax并需要该变量以在我的JavaScript中呈现部分内容时,这才成为问题。

I added 我加了

@question = Question.find(params[:question_id])

to the controller, and it fixed the problem. 控制器,它解决了问题。

In your controller do you have a respond_to do |format| 在您的控制器中,您是否有一个respond_to do |format| block? 块?

You should have something like... 你应该有...

respond_to do |format|
  if something_saves or whatever
    format.html { redirect_to random_path, notice: 'Vote was casted.' }
    format.json { render :json => { :voteCasted => true } }
  else
    format.html { render :nothing }
  end
end

The main line there is the... 主线是...

format.json { render :json => { :voteCasted => true } }

At the moment it sounds like you're missing responding to the JSON format. 目前,您似乎缺少对JSON格式的响应。 Which is why the HTML version is working fine and the AJAX (JSON) method is not. 这就是HTML版本运行良好而AJAX(JSON)方法运行不正常的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM