当我试图通过Javascript点击这个动作时,我得到一个406 Not Acceptable错误:

  def show
    @annotation = Annotation.find_by_id(params[:id])

    respond_to do |format|
      format.html {
         if @annotation.blank?
           redirect_to root_path
         else
           redirect_to inline_annotation_path(@annotation)
         end
       }

       format.js {
         if params[:format] == "raw"
           render :text => @annotation.body.to_s
         else
           render :text => @annotation.body.to_html
         end
       }
    end
  end

这是来自jQuery的,但我在发送之前做了正确的事情:

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

以下是我的请求标题:

Host    localhost:3000
User-Agent  Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10.5; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3
Accept  text/javascript
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip,deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive  300
Connection  keep-alive
X-Requested-With    XMLHttpRequest
Content-Type    application/x-www-form-urlencoded

推荐答案

I cracked the case!

我在get请求中发送了一个format参数,以便告诉服务器向我发送markdown 而不是HTML.以下是我的Javascript:

$.get("/annotations/" + annotation_id, {format: 'raw'}, function(data) {
});

然后我在format.js块中寻找这个参数:

   format.js {
     if params[:format] == "raw"
       render :text => @annotation.body.to_s
     else
       render :text => @annotation.body.to_html
     end
   }

但显然,format参数混淆了respond_to块.我把它从{format: 'raw'}改为{markdown: 'true'},效果很好.

我猜这是Rails中的一个bug?

Ruby-on-rails相关问答推荐

rails中Net::STP的多文件模式

引用连接 Rails 表中枚举的查询

ArgumentError 用于 Ruby on Rails 中非常简单的初始化方法,没有参数

ActiveRecord 回调列表

使用 RSpec 和 Capybara (Rails) 测试重定向

禁用冻结字符串文字注释判断

如果 URL 不存在,请将 http(s) 添加到 URL?

基于 2 列定义唯一主键

用 url_for 查询参数?

Rails 3:验证组合值

Heroku 错误 R14(超出内存配额):我该如何解决?

如何在不删除项目本身的情况下删除单个 HABTM 关联项目?

.increment vs += 1

Rails - 具有空数组的强参数

参数错误:范围主体需要可调用

form_for 未定义的方法 `user_path'

通过 Ruby 或 Rails 的 LDAP

在 Rails 路由中更改 id 参数

我的 JavaScript 模式/实践很糟糕.我应该go 哪里寻求帮助?

Rails:弃用警告:您没有设置 config.secret_key_base