1

我正在向我的 Rails 应用程序发送发布请求:

 $.ajax 
  url: "/" + locale + "/administration/dashboards/remove_tag_named.json"
  type: "POST"
  dataType: "json",
  contentType: "json"
  data: {"tag_name": "bingo" }
  success: (result) ->
    .....

我可以在控制台中看到帖子,但我没有成功,在帖子之后我看到列出了很多 GET 请求

[19:37:00.052] POST http://localhost:3000/fr/administration/dashboards/remove_tag_named.json [HTTP/1.1 301 Moved Permanently 42ms]
Started GET "/fr/administration%2Fdashboards%2Fremove_tag_named.json" for 127.0.0.1 at 2013-01-16 19:32:31 +0100
Started GET "/fr/administration%2Fdashboards%2Fremove_tag_named.json" for 127.0.0.1 at 2013-01-16 19:32:31 +0100
....

在我的 Rails 应用程序中,我有

  respond_to :html, :json
  .....
  def remove_tag_named
    # should remove the tag from the table
    respond_to do |format|
      format.html { redirect_to administration_dashboards_url, notice: t(:tag_deleted) }
      format.json { head :no_content }
    end
  end

有什么问题?是 jQuery ajax 请求还是 Rails 响应?如果我尝试在我的 Rails 操作中插入一个调试器,什么都不会发生..

  I have checked my routes and it's ok :

    remove_tag_named_administration_dashboards_fr POST /fr/administration/console/remove_tag_named(.:format)    administration/dashboards#remove_tag_named {:locale=>"fr"}

   remove_tag_named_administration_dashboards_en POST /en/administration/dashboards/remove_tag_named(.:format) administration/dashboards#remove_tag_named {:locale=>"en"}
4

1 回答 1

0

这一行是 ajax 响应。除了 204 状态代码之外,您不会发送任何其他内容。

format.json { head :no_content }

此外,您可能希望 *remove_tag_named* 方法具有错误条件。

def remove_tag_named
  @tag = # get tag properly
  respond_to do |format|
    if @tag.destroy
      format.html { redirect_to administration_dashboards_url, notice: t(:tag_deleted) }
      format.json { head :no_content }
    else
      format.html { # handle error condition }
      format.json { # return error info }
    end
end
于 2013-01-16T19:06:58.923 回答