动态css-错误的内容类型
我需要为我的应用程序中的每个用户添加可自定义的界面。所以在谷歌搜索之后,我决定生成css,为每个用户缓存它,并在视图中链接它。我找到了它,并以此为例 因此,我的应用程序产生了几段代码: 在controller调度主题\u controller.rb中:动态css-错误的内容类型,css,ruby-on-rails,ruby-on-rails-3,http,http-headers,Css,Ruby On Rails,Ruby On Rails 3,Http,Http Headers,我需要为我的应用程序中的每个用户添加可自定义的界面。所以在谷歌搜索之后,我决定生成css,为每个用户缓存它,并在视图中链接它。我找到了它,并以此为例 因此,我的应用程序产生了几段代码: 在controller调度主题\u controller.rb中: ... def get_css respond_to do |format| format.css { render :text => "body {\n background-color: blue; \n}", :conte
...
def get_css
respond_to do |format|
format.css { render :text => "body {\n background-color: blue; \n}", :content_type => Mime::CSS }
end
end
...
<%= stylesheet_link_tag "application" %>
<link href="<%= get_css_path %>" media="screen" rel="stylesheet" type="text/css" />
在布局中:
...
def get_css
respond_to do |format|
format.css { render :text => "body {\n background-color: blue; \n}", :content_type => Mime::CSS }
end
end
...
<%= stylesheet_link_tag "application" %>
<link href="<%= get_css_path %>" media="screen" rel="stylesheet" type="text/css" />
如您所见,响应具有content/typetext/html406错误
谁能解释一下,如果请求内容类型是text/css,为什么控制器会用text/html响应
另外,我尝试使用Google Chrome DEV HTTP客户端手动向localhost:3000/get_css发送请求。我设置了内容类型:text/css,服务器用正确的css和内容类型响应:
body {
background-color: blue;
}
所以我可能在布局中链接动态css时犯了一些错误
谢谢大家! 问题似乎出在请求的url中
将返回/get_css
,因此控制器操作通过format.html
部分处理此请求。因此,您可以尝试使用url将格式附加为:
<link href="<%= get_css_path(:format => :css) %>" media="screen" rel="stylesheet" type="text/css" />
这将请求
/get_css.css
,因此操作将处理format.css部分。问题似乎在请求的url中
将返回/get_css
,因此控制器操作通过format.html
部分处理此请求。因此,您可以尝试使用url将格式附加为:
<link href="<%= get_css_path(:format => :css) %>" media="screen" rel="stylesheet" type="text/css" />
这将请求
/get_css.css
,因此操作将处理format.css部分。尝试
它可以工作。非常感谢。您可以将此评论作为答案发布,这样我就可以将其标记为最佳答案。试试看
效果如何。非常感谢。您可以将此评论作为答案发布,以便我将其标记为最佳答案。再次感谢您的解释。再次感谢您的解释。