how to use single css for multiple views? - css

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

Related

Conditionally using .less stylesheets

I'm trying to master Less and need help.
I have three stylesheets for different parts of the same application.
Lets call them 1.less, 2.less, 3.less. I also have a shared.less stylesheet, which contains some common styles for all three parts.
I want to use these files in main stylesheet application.css via require.
How can I switch between these 1.less, 2.less and 3.less in the application.css?
Like
*= require yfu_app/shared.less
*= require yfu_app/1.less when 1st part of the app is used;
*= require yfu_app/shared.less
*= require yfu_app/2.less when 2nd part of the app is used and so on.
May be there is a better way of doing it?
EDIT
Is it possible to #import less files conditionally into shared.less?
You can add condition in your layout application.html.erb to pick stylesheet as per your app page
for an example
<%= stylesheet_link_tag 'application' %>
<% if less_1 %>
<%= stylesheet_link_tag 'less1' %>
<% elsif less_2 %>
<%= stylesheet_link_tag 'less2' %>
<% elsif less_3 %>
<%= stylesheet_link_tag 'less1' %>
<% end %>

previous page styles until refresh

I'm using different css.scss files for 2 pages in my rails app.
The issue is, when i'm going from one to another, new page weirldy obtain all styles from the previous one, until browser is refreshed.
Here is the code of one page:
<head>
<%= metamagic title: "Sample title", description: "Sample description", keywords: %w(key1 key2 key3) %>
<%= stylesheet_link_tag "posts", media: "all" %>
<%= javascript_include_tag "application" %>
<%= csrf_meta_tags %>
</head>
And if commenting out the javascript_include_tag, everything works just fine
Are there any suggestions to deal with that problem?
Thank you for your time)
The likely cause is turbolinks. If you have separate layouts on different pages, you will need to disable turbolinks on links that should cause a change in layout. Add a data-no-turbolink attribute to these links:
= link_to 'should change layouts', some_path, data: { no_turbolink: true }
This will force the full page to be loaded instead of just the content portion of the page.

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" %>

Using CSS in Rails

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.

Resources