Using CSS in Rails - css

I currently have a website with a typical homepage that uses it's own css file and then the rest of the site using one other css file. Could anyone advise on the best way to tailor my Rails app to meet these requirements?
I currently have a welcome controller with a welcome css file but not sure how to link all my other controllers to a main css file.
Thanks

Just put all your css files in
app/assets/stylesheets
then in the views use:
<%= stylesheet_link_tag "the_css_file_name_with_no_extension" %>
Actually, css files and assets in general don't get associated with controllers, but with views.
I suggest you have a read to:
http://guides.rubyonrails.org/layouts_and_rendering.html
If you want to use your controllers, you can define a global variable, and use it to conditionally include different stylesheets in your application.html.erb.
Something like:
app/views/layouts/application.html.erb
<% if #css = "main" %>
<%= stylesheet_link_tag "main" %>
<% else %>
<%= stylesheet_link_tag "application" %>
<% end%>
where #css is set to "main" in your main controller.

Related

Disabling JS/CSS bundling if debug=true in Web.config

I am using BundleTransformer to bundle and minify JS/CSS in a legacy ASP.NET WebForms app. However, I'd like to disable bundling if <compilation debug="true"> in Web.config.
Current Behavior: If debug=true, resources are still bundled together, but nothing is minified.
Desired Behavior: Bundling and minification only occur if debug=false. Otherwise, references to the individual scripts and stylesheets are rendered in the HTML.
Is this possible?
Below is an example of how I currently create one of the bundles:
BundleConfig.cs
var jsMinifier = new BundleTransformer.JsMin.Minifiers.CrockfordJsMinifier();
var customJsTransformer = new BundleTransformer.Core.Transformers.ScriptTransformer(jsMinifier);
var CustomJsBundle = new BundleTransformer.Core.Bundles.CustomScriptBundle("~/bundles/CustomJsBundle");
foreach (var item in jsFiles)
{
CustomJsBundle.Include(item);
}
CustomJsBundle.Transforms.Clear();
CustomJsBundle.Transforms.Add(customJsTransformer);
BundleTable.Bundles.Add(CustomJsBundle);
Navigation.master
<%# Master Language="C#" CodeFile="Navigation.master.cs" Inherits="Main_Nav" %>
<%# Import Namespace= "System.Web.Optimization" %>
<html>
<head runat="server">
<%= Scripts.Render("~/bundles/CustomJsBundle") %>
<!-- if debug=true, I'd like the server to just render references to the individual script files
instead of the bundle -->
</head>
<body>
<!-- other markup -->
</body>
</html>
The Scripts.Render call in Navigation.master always displays a bundle, even if debug is true. I'd like it to render the bundle only if debug is false. Otherwise, I'd like script tags to each individual script file rendered instead for debugging purposes.
I solved this for now by using the RenderFormat method when in DEBUG mode. Otherwise, I use the usual Render method.
For example, in my Navigation.master:
<% #if DEBUG %>
<%= Scripts.RenderFormat("<script src='{0}'/></script>", "~/bundles/CustomJsBundle") %>
<% #else %>
<%= Scripts.Render("~/bundles/CustomJsBundle") %>
<% #endif %>
This has the desired effect of:
In DEBUG mode, render script tags with the individual files.
Otherwise, a single script tag with the bundle is rendered.
I'm not sure if there is a better way to do this, but if so, please comment or submit an answer. I tried other potential solutions, such as setting EnableOptimizations to false, and clearing the BundleTable, but this is the only solution that has worked.

How to selectively load css in rhtml?

There are several pages generated from a same layout.rhtml. Besides a global css file, each page also has its own css file, say page1.css, page2.css corresponding to page1.rhtml, page2.rhtml. Is there any way to seletively load the css files in rhtml?
Thanks in advance.
Define a instance variable in controller for which css to load. lets say we will use stylefile variable in controller like this:
#stylefile = "page1.css"
Now write following code where you load css file.
<%= stylesheet_link_tag #stylefile %>
Similarly change filename in controller for page2.
If i understood correctly,you can just include them with stylesheet_link_tag like this
<%= stylesheet_link_tag 'page1' %>
<%= stylesheet_link_tag 'page2' %>
in the .rhtml files.

Stylesheets in Rails

I would like to use one of my stylesheets from the public folder (several directories each with their own css file) for one of my controllers.
Having trouble bringing in the static css file.
I've tried the following code -
<%= stylesheet_link_tag "/public/foldername/style2.css" %>
But it's not working
Please try
<%= stylesheet_link_tag "/foldername/style2.css" %>

how to use single css for multiple views?

I am creating an application where I am having many views but they all have to use a single css...
I placed my css code in assests\stylesheets\application.css
and in my view head part I am adding:
<%= stylesheet_link_tag 'application.css' %>
but stil it is not working
Try using:
<%= stylesheet_link_tag 'application' %>
Note the lack of .css
See this answer https://stackoverflow.com/a/10492888/950890

ASP.NET MVC Modify CSS links via code (runtime/development)?

does anyone know of a good way of doing the following, I need to have available via CSS Links up to 5 CSS files in one, this is pretty bad for trips to the server.. but it helps me keep it organized...
At runtime i automatically consolidate and minify all css files into 1 ...
What i was wondering is how to have links to css files in my source at design time and different at runtime??
Is there some special workaround i can do with <% %> tags?
Thanks
You could have one link to the combined file in your HTML and use a build event to combine your separate ones in to the one file. That way, during dev you always see your neat separate files but the designer (and at runtime) will always see the combined file. The designer doesn't care of there is one or 5 files but you do. This way you don't have to do any conditional design-time only logic and your code will be cleaner.
I use if (false) with HtmlHelper extensions to achieve similar effects. It might look like:
<% if (false) { %>
<link href="../content/styles/jquery-ui.css" ...
<link href="../content/styles/site.css" ...
<% } %>
<%= Html.CSS( "jquery-ui.css", "site.css", ... ) %>
You can try the following in your view or master page:
1) Leave the min'ed CSS links in as they are
2) Use this conditional block to include the CSS files directly as needed for design time:
<% if (this.DesignMode) { %>
<link rel="stylesheet" type="text/stylesheet" href="css/styles.css" />
<% } %>

Resources