开发者

Rails - is there a way to get Rails to render a Style .css file inside of the view?

Instead of the page making another request for the CSS, I would like to have the Rails view render the CSS file 开发者_高级运维in the page, so it's only 1 request.

Is this possible?


Altough the typical way to include CSS is using stylesheet_link_tag (in particular so it gets cached by the client), it is possible to put it directly in your <HEAD> in Rails 3.1:

<HEAD>
  ....
  <STYLE type="text/css">
    <%= YouAppName::Application.assets["your_stylesheet.css"].to_s.html_safe %>
  </STYLE>
</HEAD>

I adapted this from this post.


<%= stylesheet_link_tag :all %> assuming your CSS file is in your public/stylesheets folder (which is the conventional place to store stylesheets). Of course, instead of :all, you can specify a specific file.

in your page's block.

I usually do this in my application.html.erb (which is a layout in your app/views/layouts) folder. But you can do this in any view file with a block.

Good luck!


Just wrap your CSS in

<style></style>

within your view.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜