406 jQuery上不可接受的错误:remote=>;真实链接

406 jQuery上不可接受的错误:remote=>;真实链接,jquery,ruby-on-rails-3,http-status-codes,http-status-code-406,Jquery,Ruby On Rails 3,Http Status Codes,Http Status Code 406,我通过一个锚标记链接提交一个删除请求,并使用:remote=>true通过JS提交请求以使用jQuery。我在另外两个例子中也做过同样的事情,没有任何问题。但由于某些原因,这一个是造成问题-每当我提交我得到一个406不可接受的错误 销毁链接 content_tag( :p, link_to("+#{vote.weight}", unvote_path(vote), :method => :delete, :remote => true ), :class => "chosen"

我通过一个锚标记链接提交一个删除请求,并使用:remote=>true通过JS提交请求以使用jQuery。我在另外两个例子中也做过同样的事情,没有任何问题。但由于某些原因,这一个是造成问题-每当我提交我得到一个406不可接受的错误

销毁链接

content_tag( :p, link_to("+#{vote.weight}", unvote_path(vote), :method => :delete, :remote => true ), :class => "chosen" )
routes.rb

delete "/unvote" => "votes#destroy", :as => :unvote
def destroy
    @vote = Vote.find(params[:format])
    if !current_user.owns(@vote)
      flash[:alert] = "You cannot remove votes that aren't yours!"
    end
    @idea = @vote.idea
    @vote.destroy

    respond_with @vote do |format|
      format.js
      format.html { redirect_to category_idea_path(@idea.category, @idea) }
    end
end
$('#vote_buttons').append('<%= escape_javascript get_vote_buttons(@idea.category, current_user, @idea) %>');
投票\u controller.rb

delete "/unvote" => "votes#destroy", :as => :unvote
def destroy
    @vote = Vote.find(params[:format])
    if !current_user.owns(@vote)
      flash[:alert] = "You cannot remove votes that aren't yours!"
    end
    @idea = @vote.idea
    @vote.destroy

    respond_with @vote do |format|
      format.js
      format.html { redirect_to category_idea_path(@idea.category, @idea) }
    end
end
$('#vote_buttons').append('<%= escape_javascript get_vote_buttons(@idea.category, current_user, @idea) %>');
destroy.js.erb

delete "/unvote" => "votes#destroy", :as => :unvote
def destroy
    @vote = Vote.find(params[:format])
    if !current_user.owns(@vote)
      flash[:alert] = "You cannot remove votes that aren't yours!"
    end
    @idea = @vote.idea
    @vote.destroy

    respond_with @vote do |format|
      format.js
      format.html { redirect_to category_idea_path(@idea.category, @idea) }
    end
end
$('#vote_buttons').append('<%= escape_javascript get_vote_buttons(@idea.category, current_user, @idea) %>');
我在application.js中看到了这一点,如果我理解正确的话,应该正确设置标题

另外,请注意,链接正确执行其删除功能-当我手动刷新页面时,投票已被删除。唯一的问题似乎是destroy.js.erb返回的内容

delete "/unvote" => "votes#destroy", :as => :unvote
def destroy
    @vote = Vote.find(params[:format])
    if !current_user.owns(@vote)
      flash[:alert] = "You cannot remove votes that aren't yours!"
    end
    @idea = @vote.idea
    @vote.destroy

    respond_with @vote do |format|
      format.js
      format.html { redirect_to category_idea_path(@idea.category, @idea) }
    end
end
$('#vote_buttons').append('<%= escape_javascript get_vote_buttons(@idea.category, current_user, @idea) %>');

你知道为什么这与我的其他工作示例不同吗?我整天都在绞尽脑汁来解决这个问题。

你不需要在销毁链接中添加:method=>:delete吗?另外,您可能不想使用新的\u category\u idea\u vote\u路径来生成销毁链接,而只是使用category\u idea\u vote\u路径

就像这样:

content_tag( :p, link_to("+#{i}", category_idea_vote_path(category, idea, :weight => i), :remote => true, :method => :delete ), :class => "valid" )
请参见此示例:

  link_to("Destroy", "http://www.example.com", :method => :delete, :confirm => "Are you sure?")

对不起,Stefan。我复制了一段错误的代码——我原来在那里有一个新的投票链接,它可以正常工作。我已经用正确的destroy链接和routes.rb中的相关行更新了这个问题,以定义unnote_路径。如果你能帮我再看一眼,我将不胜感激!还要注意的是,该链接完美地执行了其删除功能-当我手动刷新页面时,投票已被正确删除。您是否使用firebug检查了您发送和接收的请求类型?同时检查您的日志,您收到的请求应该类似于*/*响应标题:内容类型text/html;字符集=utf-8。请求标头:Accept/;q=0.5,text/javascript,application/javascript。我已经知道这是个问题,但我无法让它返回正确的请求(text/javascript),即使它的功能与我的其他删除链接完全相同。好吧,我在这里的实现与我的其他删除链接并不完全相同。问题似乎出在
:unnote
路径上-当我删除它,而使用
类别、想法、投票、路径(etc)
时,它起了作用。我不知道是否有办法让它使用自定义路线,但这似乎是一个我试图太聪明而它又回来咬我的例子:(