Rails路由与IE一样糟糕 [英] Rails routing is being wonky with 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版本: http://host.name/channel/sweetchatroom
CSV版本: http://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:
http://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一样糟糕的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!