1

我正在通过锚标记链接提交删除请求,并使用 :remote => true 通过 JS 提交它以使用 jQuery。我在其他两个与此相同的情况下完成了此操作,完全没有问题。但由于某种原因,这会导致问题 - 每当我提交时,我都会收到 406 Not Acceptable 错误。

破坏链接

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

路线.rb

delete "/unvote" => "votes#destroy", :as => :unvote

votes_controller.rb

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

销毁.js.erb

$('#vote_buttons').append('<%= escape_javascript get_vote_buttons(@idea.category, current_user, @idea) %>');

这与我在new.js.erb中的行完全相同,并且工作正常(除了它没有完全返回我想要的内容,但它至少可以正确执行和附加)。

应用程序.js

jQuery.ajaxSetup({
    'beforeSend': function(xhr) { 
        xhr.setRequestHeader("Accept", "text/javascript");
    },
    cache: false
 });

如果我理解正确的话,我在application.js中得到了它应该正确设置标题。

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

知道为什么这与我的其他工作示例不同吗?我整天都在为此苦苦挣扎,无法弄清楚。

4

1 回答 1

0

您不必在销毁链接中添加 :method => :delete 吗?此外,您可能不想使用 new_category_idea_vote_path 来生成销毁链接,而只是使用 category_idea_vote_path

像那样:

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?")

http://api.rubyonrails.org/classes/ActionView/Helpers/UrlHelper.html#method-i-link_to

于 2011-02-07T13:16:33.623 回答