What's the layouts folder for in Rails? - ruby-on-rails

I understand that .html.erb files go within app/views or its subfolders. But what is the app/views/layouts folder in particular for in Rails?

app/views/layouts is the folder in which rails looks for the layouts.
From http://guides.rubyonrails.org/layouts_and_rendering.html#finding-layouts :
To find the current layout, Rails first looks for a file in
app/views/layouts with the same base name as the controller. For
example, rendering actions from the PhotosController class will use
app/views/layouts/photos.html.erb (or
app/views/layouts/photos.builder). If there is no such
controller-specific layout, Rails will use
app/views/layouts/application.html.erb or
app/views/layouts/application.builder. If there is no .erb layout,
Rails will use a .builder layout if one exists. Rails also provides
several ways to more precisely assign specific layouts to individual
controllers and actions.
What is a layout?
A layout defines the surroundings of an HTML page. It's the place to define common look and feel of the page.
The RailsCasts episode - All About Layouts - though very old, is still very useful in this context.

Layout in rails framework is very important folder, main layout of your application is define here as application.html.erb and all the views are yielded here using <% yield %>

Related

How to seperate asset pipeline with different layouts

I have a new project, and I have created 2 layouts for it.
The 1st layout is the main layout used for the application.
The 2nd layout is for the 'beta' landing page, where users can signup via email to recieve updates on the website. I am using bootstrap for this section.
My assets folder looks like:
/assets/
/assets/bootstrap/
/assets/.... (default folders generated by rails 4)
Is it possible for asset pipeline to server the assets for the 'beta' layout from the /assets/bootstrap tree?
You will have to use two layout files in app/views/layouts: one you just leave like it is and then you create a second that will be called something like application_bootstrap.html.erb. In the second one you will include a stylesheet link tag like <%= stylesheet_link_tag "bootstrap" %>. In the assets directory you will have the regular application.css where you have to make sure not to include the assets/bootstrap directory and a new bootstrap.css which simply requires the assets/bootstrap/ tree to be included.
Then in you controllers you can switch the layouts: layout application_bootstrap and it will include the new layout file which in return includes the new CSS that you created. Of course you could also switch the layout in the application_controller.rb based on user settings or whatever you choose.

Relative filename in ERB files

I am putting my ERB files in app/view. The problem is that sometimes I make mistakes when including CSS and JS files. I refer to them as "js/include.js" or "css/default.css" instead of /js/include.js and /css/default.css
However, these files are located in the public directory not the app/views directory so as a result the page breaks.
Is there a way to change the default behavior so that it looks in public folder whenever I refer to these files relatively?
If you stick with the Rails conventions you'll save yourself some grief. Use stylesheet_link_tag and javascript_include_tag in your layouts. And don't scatter css and js includes around your views. Use custom layouts instead.
For example, if you have an admin interface with a different look and different behavior you could add app/views/layouts/admin.html.erb. Then, in AdminController specify that it should use the admin layout. You can also specify the layout at the controller action level if you need to.

Defining separate views and assets for admin interface in a Rails app

I'm trying to build a CMS for my site in Rails and I want to have separate styles for all the views that people reading my site will be able to see, namely index and show views, and the views that handle creating, updating and deleting views. I understand that I should probably separate these two areas out into separate controllers and namespace all the admin ones' routes, but I'm at a loss on how to do the views.
Is there any way to specify a layout, including stylesheets and javascript files, for a specific set of controllers? Note I'm using Rails 3.1 so as things are right now all my stylesheets and scripts get compiled into single files that are served with every view.
I was running around looking for a very similar thing. And was lucky enough to find a great tutorial article by Iain Hecker.
Backends in Rails 3.1
Its set up using Namespacing and Template Inheritance. And the best part is he uses the inherited_resources gem by Jose Valim which really cleans up your controllers.
Anyway, it really got me on the right track.
Also, what will help is in your default application.js make sure to use:
//= require_directory .
instead of
//= require_tree
This will make sprockets only load files in the current directory. Then you can add an admin/ folder with its own application.js file doing the same thing.
Then of course in you layouts/application.html.erb you use:
<%= javascript_include_tag "application" %>
And in layouts/admin/application.html.erb:
<%= javascript_include_tag "admin/application" %>
Hope that helps...
Adam.
For CSS, I like to add some body classes in my application.html.erb that allow me to separate a) admin from non-admin, and b) the various controllers/actions from each other. Here's what my body tag often looks like
<body class="<%= 'admin' if admin? %> <%= params[:controller].parameterize %> <%= params[:controller].parameterize %>_<%= params[:action].parameterize %>">
where admin? is an ApplicationHelper method defined as follows:
def admin?
controller.class.name.split("::").first=="Admin"
end
For JS, you need to tackle view specific javascript. For that I'm going to point you here. The multiple manifest file technique discussed there could be useful for you, as you could construct an admin.js file that contains everything you need within your Admin namespace.
UPDATE: I thought this was a good question, so I wrote up a more thorough response here
One way of getting what I think you want would be to use a <div id="index"> index stuff ....
and then use nested layouts to style the div

Where do you put small snippets of CSS codes for rails app?

I know that most CSS codes go under app/assets/stylesheets, but I have some snippets of CSS codes that are specific to only certain pages. For now, I just have these small CSS codes included in the view files, but I feel like there's ought to be a better way of handling this.
Any suggestion?
Rails convention is to put these in controller specific CSS files:
For example, if a ProjectsController is generated, there will be a new
file at app/assets/javascripts/projects.js.coffee and another at
app/assets/stylesheets/projects.css.scss. You should put any
JavaScript or CSS unique to a controller inside their respective asset
files, as these files can then be loaded just for these controllers
with lines such as <%= javascript_include_tag params[:controller] %>
or <%= stylesheet_link_tag params[:controller] %>.
Putting the CSS inside the views isn't a good idea as you lose features (fingerprinting, auto minification) that the asset management in Rails provides.
Read more here: http://guides.rubyonrails.org/asset_pipeline.html#how-to-use-the-asset-pipeline

Rails Routing INSIDE CSS

Is there any way I can route assets inside of my css to where the rest of the views are pulling them? I mean, inside the CSS can I call url_for or css_for or something like that in order to have the images go through the assets router?
Thank you in advance!
You can use a controller action to render your CSS (with an erb template) and set the content type to text/css.
Take a look at this blog post from Josh Susser on dynamically generated stylesheets. It is from 2006 but the technique described is still applicable.

Resources