Rails路由与IE一样糟糕

编程入门 行业动态 更新时间:2024-10-26 00:23:17
本文介绍了Rails路由与IE一样糟糕的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

想要使用jQuery,Orbited和FasterCSV,我制作了一个Rails聊天应用程序。

Wanting to play with jQuery, Orbited, and FasterCSV, I made a Rails chat application.

您可以浏览到一个URL,并且有一个类似于IRC的聊天窗口。您还可以通过访问相同的URL导出聊天窗口的内容,但为URL添加.csv扩展名。

You can browse to a URL and there is a chat window that is similar to IRC. You can also export the contents of the chat window by visiting the same URL but adding a ".csv" extension to the URL.

HTML版本: host.name/channel/sweetchatroom

CSV版本: host.name /channel/sweetchatroom.csv

在Firefox,Safari和Chrome中,它正常工作。在IE中,如果我访问HTMLURL,则会获得该页面的CSV版本。我必须手动将.html添加到URL中,如下所示:

In Firefox, Safari, and Chrome it works normal. In IE, If I visit the "HTML" URL, I get the CSV version of the page. I have to manually add ".html" to the URL like so:

host.name/channel/sweetchatroom.html

我的路线目前看起来像这样:

My route currently looks like this:

map.chat '/channel/:name.:format', :controller => 'channels', :action => 'show'

我用Google搜索并尝试了以下建议:

I Googled a bit and tried the following suggestions:

map.slug '/channel/:slug.:format', :controller => 'channels', :action => 'show', :defaults => {:format => 'html'}

- 和 -

map.slug '/channel/:slug.:format', :controller => 'channels', :action => 'show', :format => 'html'

它们都没有奏效。显然,如果您在未指定格式的情况下访问URL,则Rails不会将 params [:format] 设置为任何内容。原则上我更喜欢这个,但是文档很清楚,你可以设置一个默认格式,我不知道为什么它不尊重这个。 :defaults => ...建议就是Rails文档中的内容。

Neither of them worked. Apparently, if you visit a URL without specifying the format, Rails does not set params[:format] to anything. Which in principle I prefer, but the docs are pretty clear that you can set a default format and I'm not sure why it doesn't honor this. The ":defaults => ..." suggestion is what is in the Rails docs.

为了让它工作,我不得不改变我的部分渠道控制器:

In order to get it to work I had to change this part of my channels controller:

respond_to do |format| format.csv { send_data channel_to_csv(@channel), :type => "text/plain", :filename => "#{@channel.slug}.csv", :disposition => 'inline' } format.html # show.html.erb format.xml { render :xml => @channel } end

到此:

respond_to do |format| format.csv { send_data channel_to_csv(@channel), :type => "text/plain", :filename => "#{@channel.slug}.csv", :disposition => 'inline' } if params[:format] == 'csv' # <-- Here is the change format.html # show.html.erb format.xml { render :xml => @channel } end

它运作完美但看起来真的很乱。必须有更好的,更红宝石的方式。我的路线条目上的语法有误吗?似乎路线应该是这样的。

It works perfectly but seems really hackish. There has to be a better, more "Ruby" way. Do I have the syntax wrong on my routes entry? It seems like routes is where this should be.

我知道我必须遗漏一些东西。我无法在Google或StackOverflow上找到有关此问题的良好信息。这通常意味着我已经走出了杂草。

I know I have to be missing something. I couldn't find good information on this problem on Google or on StackOverflow. That generally means I'm way out in the weeds.

推荐答案

我通常只把format.html放在第一位。这样,当IE发送一个奇怪的接受标题(如 * / * )时,它不会被搞砸。基本上,如果IE说它接受了所有内容(比如URL上没有扩展名),Rails会发送第一个匹配的东西。

I usually just put the format.html first. That way, when IE sends a weird accepts header (like */*), it doesn't get fouled up. Basically, if IE says it accepts everything (like when there is no extension on the URL) Rails will send it the first thing that matches.

更多推荐

Rails路由与IE一样糟糕

本文发布于:2023-07-16 23:07:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1126878.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   糟糕   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!