Rails 3:尝试使用 JavaScript 加载内容后,移动 MIME 类型抛出 406 错误

发布于 2024-12-20 04:29:51 字数 3750 浏览 1 评论 0原文

我正在关注 Railscast #199 以允许在移动浏览器中查看我的网络应用程序。它工作得很好,除非我尝试在移动版本中使用 UJS 访问选项卡式界面中的信息。单击选项卡可以在 Web 应用程序中使用,但在移动端我收到 406 错误。 (我在 Safari 中将用户代理设置为 iPhone 后尝试了此操作。我还在 iOS 模拟器和我的 iPhone 上进行了测试。两次都没有加载任何内容。)

下面是其中一个选项卡的一些代码。任何人都可以帮助我确定正在发生的事情吗?这是我的代码。

这是 profiles_controller.rb 中的 profile_about 操作:

def profile_about
  @profile = Profile.find(params[:id])
  respond_to do |format|
    format.js { render :layout => nil }
  end
end

在我的 profiles/show.mobile.erb 中(这与以下代码完全相同)在 profiles/show.html.erb 中):

<div id="tabs">
  <ul id="infoContainer">
    <li><%= link_to "Cred", profile_cred_profile_path, :class=> 'active', :remote => true %></li>
    <li><%= link_to "About", profile_about_profile_path, :class=> 'inactive', :remote => true %></li>
  </ul>
  <div id="tabs-1">
  <%= render :partial => 'profile_cred' %>
  </div>
</div><!-- end tabs -->

(注意:我有一个 profiles/_profile_about.html.erb 的文件,并且profiles/_profile_about.mobile.erb。)

这是我的profiles/profile_about.js.erb

$("#tabs-1").html("<%= escape_javascript(render(:partial => 'profile_about'))%>");

我的 Heroku显示 406 的日志:

2012-03-08T03:02:55+00:00 app[web.1]: Started GET "/profiles/1/profile_about" for 98.218.231.113 at 2012-03-08 03:02:55 +0000
2012-03-08T03:02:55+00:00 heroku[router]: GET myapp.com/profiles/1/profile_about dyno=web.1 queue=0 wait=0ms service=14ms status=406 bytes=1
2012-03-08T03:02:55+00:00 app[web.1]:   Processing by ProfilesController#profile_about as JS
2012-03-08T03:02:55+00:00 app[web.1]:   Parameters: {"id"=>"1"}
2012-03-08T03:02:55+00:00 app[web.1]: Completed 406 Not Acceptable in 3ms
2012-03-08T03:02:55+00:00 heroku[nginx]: 98.218.231.113 - - [08/Mar/2012:03:02:55 +0000] "GET /profiles/1/profile_about HTTP/1.1" 406 1 "http://myapp.com/profiles/1" "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3" myapp.com

来自运行 tail -f logs/development.log

Started GET "/profiles/1/profile_about" for 127.0.0.1 at Wed Mar 07 22:35:36 -0500 2012
  Processing by ProfilesController#profile_about as JS
  Parameters: {"id"=>"1"}
  PK and serial sequence (5.4ms)   SELECT attr.attname, seq.relname
 FROM pg_class seq,
 pg_attribute attr,
 pg_depend dep,
 pg_namespace name,
 pg_constraint cons
 WHERE seq.oid = dep.objid
 AND seq.relkind = 'S'
 AND attr.attrelid = dep.refobjid
 AND attr.attnum = dep.refobjsubid
 AND attr.attrelid = cons.conrelid
 AND attr.attnum = cons.conkey[1]
 AND cons.contype = 'p'
 AND dep.refobjid = '"goals_profiles"'::regclass
  PK and custom sequence (2.5ms)   SELECT attr.attname,
 CASE
 WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN
 substr(split_part(def.adsrc, '''', 2),
 strpos(split_part(def.adsrc, '''', 2), '.')+1)
 ELSE split_part(def.adsrc, '''', 2)
 END
 FROM pg_class t
 JOIN pg_attribute attr ON (t.oid = attrelid)
 JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum)
 JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])
 WHERE t.oid = '"goals_profiles"'::regclass
 AND cons.contype = 'p'
 AND def.adsrc ~* 'nextval'

  Profile Load (1.3ms)  SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = '1' LIMIT 1
Completed 406 Not Acceptable in 30ms

I'm following Railscast #199 to allow my web app to be viewed in a mobile browser. It works great, except when I try to access information in a tabbed interface using UJS in the mobile version. Clicking on the tabs works in the web app, but on the mobile side I get a 406 error. (I tried this after setting the User Agent as iPhone in Safari. I also tested on iOS Simulator and my iPhone. Neither time loaded anything.)

Below is some code for one of the tabs. Can anyone can help me target what is going on? Here is my code.

Here's the profile_about action in profiles_controller.rb:

def profile_about
  @profile = Profile.find(params[:id])
  respond_to do |format|
    format.js { render :layout => nil }
  end
end

In my profiles/show.mobile.erb (this is the exact same code as in profiles/show.html.erb):

<div id="tabs">
  <ul id="infoContainer">
    <li><%= link_to "Cred", profile_cred_profile_path, :class=> 'active', :remote => true %></li>
    <li><%= link_to "About", profile_about_profile_path, :class=> 'inactive', :remote => true %></li>
  </ul>
  <div id="tabs-1">
  <%= render :partial => 'profile_cred' %>
  </div>
</div><!-- end tabs -->

(NOTE: I have a file for profiles/_profile_about.html.erb and profiles/_profile_about.mobile.erb.)

Here is my profiles/profile_about.js.erb:

$("#tabs-1").html("<%= escape_javascript(render(:partial => 'profile_about'))%>");

My Heroku logs showing the 406:

2012-03-08T03:02:55+00:00 app[web.1]: Started GET "/profiles/1/profile_about" for 98.218.231.113 at 2012-03-08 03:02:55 +0000
2012-03-08T03:02:55+00:00 heroku[router]: GET myapp.com/profiles/1/profile_about dyno=web.1 queue=0 wait=0ms service=14ms status=406 bytes=1
2012-03-08T03:02:55+00:00 app[web.1]:   Processing by ProfilesController#profile_about as JS
2012-03-08T03:02:55+00:00 app[web.1]:   Parameters: {"id"=>"1"}
2012-03-08T03:02:55+00:00 app[web.1]: Completed 406 Not Acceptable in 3ms
2012-03-08T03:02:55+00:00 heroku[nginx]: 98.218.231.113 - - [08/Mar/2012:03:02:55 +0000] "GET /profiles/1/profile_about HTTP/1.1" 406 1 "http://myapp.com/profiles/1" "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0_1 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A405 Safari/7534.48.3" myapp.com

From running tail -f logs/development.log:

Started GET "/profiles/1/profile_about" for 127.0.0.1 at Wed Mar 07 22:35:36 -0500 2012
  Processing by ProfilesController#profile_about as JS
  Parameters: {"id"=>"1"}
  PK and serial sequence (5.4ms)   SELECT attr.attname, seq.relname
 FROM pg_class seq,
 pg_attribute attr,
 pg_depend dep,
 pg_namespace name,
 pg_constraint cons
 WHERE seq.oid = dep.objid
 AND seq.relkind = 'S'
 AND attr.attrelid = dep.refobjid
 AND attr.attnum = dep.refobjsubid
 AND attr.attrelid = cons.conrelid
 AND attr.attnum = cons.conkey[1]
 AND cons.contype = 'p'
 AND dep.refobjid = '"goals_profiles"'::regclass
  PK and custom sequence (2.5ms)   SELECT attr.attname,
 CASE
 WHEN split_part(def.adsrc, '''', 2) ~ '.' THEN
 substr(split_part(def.adsrc, '''', 2),
 strpos(split_part(def.adsrc, '''', 2), '.')+1)
 ELSE split_part(def.adsrc, '''', 2)
 END
 FROM pg_class t
 JOIN pg_attribute attr ON (t.oid = attrelid)
 JOIN pg_attrdef def ON (adrelid = attrelid AND adnum = attnum)
 JOIN pg_constraint cons ON (conrelid = adrelid AND adnum = conkey[1])
 WHERE t.oid = '"goals_profiles"'::regclass
 AND cons.contype = 'p'
 AND def.adsrc ~* 'nextval'

  Profile Load (1.3ms)  SELECT "profiles".* FROM "profiles" WHERE "profiles"."id" = '1' LIMIT 1
Completed 406 Not Acceptable in 30ms

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心碎的声音 2024-12-27 04:29:51

您的代码中有一些错误,也许只是这里的 stackoverflow 格式,但内部引号应该是 ' 而不是 " ,如下所示:

$("#tabs-1").html("<%= escape_javascript(render(:partial => 'profile_about'))%>");

这行会导致您的错误:

format.mobile.js {render :layout => nil}

这是不可能的,因为请求只能有一个 mime 类型。 “mobile”或“js”,而不是两者。如果您从 javascript 请求“profile_about”操作,则必须使用 js 来响应它。 “profile_about.mobile”模板

希望这至少是您朝着正确方向迈出的一步。

There's a few bugs in your code, maybe it's just stackoverflow formatting here but the inner quotes should be ' instead of " , like this:

$("#tabs-1").html("<%= escape_javascript(render(:partial => 'profile_about'))%>");

And this line is casuing your error:

format.mobile.js {render :layout => nil}

This is impossible because the request can only have a single mime type. "mobile" or "js", not both. If you are requesting the "profile_about" action from javascript, then you must respond back to it with js. "format.mobile" should only be used to render a "profile_about.mobile" template.

Hopefully that's at least a step in the right direction for you.

趁微风不噪 2024-12-27 04:29:51

可能没有完全回答您的问题,但我遇到了 406 状态问题,因为我一直将我的应用程序部署到 Phonegap!
发生这种情况的主要原因是请求类型与操作中的任何 Rails 响应程序都不匹配。

就像:

respond_to do |format|
  format.json {
    render( json: (@parishes) )
  }
end

因为我只用它来响应 JSON,所以我改变了,它现在可以工作了:

    render( json:(@parishes) )

处理这个问题的更完整的方法是准确地计算出该请求的响应者,或者默认为你知道可以工作的东西

Probably don't fully answer your question but I was having the 406 status problem, because I've been deploying my app to Phonegap!
It basically happened because the request type didn't match any of the Rails responders in the action.

it was like:

respond_to do |format|
  format.json {
    render( json: (@parishes) )
  }
end

Since I only used it to responde to JSON I changed and it now works:

    render( json:(@parishes) )

A more complete way to deal with this is to figure exactly what responder is being asked for that request, or default to something you know to work

命比纸薄 2024-12-27 04:29:51

事实证明,这是由于 prepare_for_mobile 方法中缺少对 xhr 请求的检查。我在另一个问题中找到了答案。所以下面的 prepare_for_mobile 方法允许 JS 工作:

def prepare_for_mobile
  session[:mobile_param] = params[:mobile] if params[:mobile]
  request.format = :mobile if mobile_device? && !request.xhr?
end

Turns out this was due to a lacking check for an xhr request in the prepare_for_mobile method. I found the answer in another question. So the below prepare_for_mobile method allows the JS to work:

def prepare_for_mobile
  session[:mobile_param] = params[:mobile] if params[:mobile]
  request.format = :mobile if mobile_device? && !request.xhr?
end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文