How to selectively load css in rhtml? - css

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.

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

What does the form-inputs HTML class do in simple_form?

Doing a code search for "form-inputs" in the simple_form github repo indicates it's added as an HTML class in generated templates for ERB, slim, and erb. For example, in the HAML template file _form.html.haml:
= simple_form_for(#<%= singular_table_name %>) do |f|
= f.error_notification
.form-inputs
<%- attributes.each do |attribute| -%>
= f.<%= attribute.reference? ? :association : :input %> :<%= attribute.name %>
<%- end -%>
.form-actions
= f.button :submit
However, I can't see the HTML class being used within simple_form or elsewhere (such as Twitter Bootstrap). For example, I can't find any CSS files changing the styling of divs with the form-inputs class.
What is the purpose of the form-inputs class?
This was ostensibly added to "make it easier to integrate with bootstrap". It replaces a similar wrapper div with a different class. As you've pointed out, this is more relevant for form-actions than for form-inputs, so the latter is most likely just a convenient way to encapsulate all of the fields in a consistent way.

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.

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

Resources