想要使用jQuery,绕行,并FasterCSV玩,我做了一个Rails的聊天应用程序。
您可以浏览到一个URL并且有类似于IRC聊天窗口。 您也可以访问相同的URL,但加入了名为“.csv”扩展名的URL导出聊天窗口中的内容。
HTML版本: http://host.name/channel/sweetchatroom
CSV版本: http://host.name/channel/sweetchatroom.csv
在Firefox,Safari和Chrome浏览它工作正常。 在IE中,如果我参观“HTML”的网址,我得到的页面的CSV版本。 我要“ html的”手动添加到像这样的网址:
http://host.name/channel/sweetchatroom.html
我的路线目前看起来是这样的:
map.chat '/channel/:name.:format', :controller => 'channels', :action => 'show'
我用Google搜索了一下,尝试了以下建议:
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]
任何东西。 这在原则上我比较喜欢,但该文档是很清楚,你可以设置一个默认的格式,我不知道为什么它不接受这一点。 该“:默认设置=> ...”的建议是什么在Rails的文档。
为了得到它的工作,我不得不改变我的信道控制器的这一部分:
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
它完美的作品,但似乎真正的hackish。 必须有一个更好的,更多的“红宝石”的方式。 我是否有语法错误在我的路线进入? 好像途径就是,这应该是。
我知道我必须失去了一些东西。 我找不到对谷歌或在计算器上这个问题的好资料。 这通常意味着我的出路在杂草。