页面特定的CSS与Rails应用程序 [英] Page-specific CSS with Rails App

查看:94
本文介绍了页面特定的CSS与Rails应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为我的Rails应用程序测试两个不同的接口。理想情况下,我想有两个单独的css文件,一个与每个接口相关联(例如,new_interface.css和old_interface.css)。我目前有两个不同的部分的接口,一个名为_new_interface.html.erb和一个名为_old_interface.html.erb。如果我想调用正确的CSS文件,当一个特定的视图加载,我该怎么做?例如,如果我加载_new_interface.html.erb,我想要加载new_interface.css文件并忽略old_interface.css文件。



我的application.css:

  / * 
* = require_tree
* /


解决方案

这个问题似乎是针对你需要做的:使用Rails 3.1资产管道有条件地使用某些css



简而言之,您需要将app / assets / stylesheet文件夹重新组织为某些子目录,并更改您的清单文件,以便在运行时不要将所有绑定在一起。



然后,您可以在视图中放置一些条件逻辑,以便为作业加载正确的css。 content_for标签可能会在这里有用。您可以编辑app / views / layouts / application.html.erb,并在其他JavaScript<%= yield:view_specific_css%>之后加入一行。然后在您的视图文件中,您可以使用

 <%content_for:view_specific_css do%> 
<%= stylesheet_link_tagwhatever%>
<%end%>


I would like to test out two different interfaces for my Rails application. Ideally, I'd like to have two separate css files, one associated with each interface (e.g., new_interface.css and old_interface.css). I currently have two different partials for the interfaces, one called _new_interface.html.erb and one called _old_interface.html.erb. If I'd like to invoke the correct css file when a particular view is loaded, how would I do this? For example, if I load _new_interface.html.erb, I want it to load the new_interface.css file and ignore the old_interface.css file.

My application.css:

/*
*= require_tree
*/

解决方案

This question seems to be aimed at what you need to do: Using Rails 3.1 assets pipeline to conditionally use certain css

In a nutshell, you need to reorganize your app/assets/stylesheet folder into some subdirectories and change your manifest files so that not everything gets bundled together at run time.

Then you can put some conditional logic in your view so that it loads the right css for the job. The content_for tag is probably going to be useful here. You could edit app/views/layouts/application.html.erb and include a line just after the other javascript <%= yield :view_specific_css %>. Then in your view file you can use

<% content_for :view_specific_css do %>
    <%= stylesheet_link_tag "whatever %>
<% end %>

这篇关于页面特定的CSS与Rails应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆