DEV Community

Austin Harlow
Austin Harlow

Posted on

Rails Layouts

In my coding bootcamp we have started the Rails section of the curriculum. Last week it seemed very overwhelming, as if there was so much Rails content that I would not be able to understand enough to build an app.

At the start of this week, I still feel that I have barely scraped the surface of what Rails can really do. However, I did find one thing in particular that I really enjoy about the Rails apps we have made so far.

Layouts

Layouts allow you to do a great many things with your application but to keep it simple, lets break down three simple ways that a layout can impact your application.

Default Layout

This is the layout that will persist throughout your application. A default layout may contain a link to your homepage, a login/logout link, and a help link. This might look something like this.

<%= link_to 'Home', root_path %>
<%= link_to 'Login', login_path %>
<%= link_to 'Help', help_path %>
<%= yield %>

Rendering a layout

Much like how you can render a view page, you can render a specific layout as well. For example, you may want to have a different layout on your login page than you do for your homepage. Rendering a layout uses a very similar syntax to rendering a specific view.

render layout: 'login'

Controller vs Method

By default your controller will look for a layout file that matches the controller model. For example, for your users controller, Rails will look for a users view file in your layouts folder. However, if you want to use a specific layout for one of your methods, you can still specify that with the render layout command that we saw above.

Moving Forward

Layouts are an excellent way of keeping your code dry as something such as a 'Home' link might be found on nearly every page of your app. They also have the benefit of allowing you to include links that help you to navigate and test your app quickly. While it may not seem like much, I very much appreciate the ability to navigate my apps directly without having to manually enter URLs.

Top comments (1)

Collapse
 
andrewbrown profile image
Andrew Brown 🇨🇦

If you want to set a controller to utilize a specific layout for all its actions
Notice its

layout 'admin'

instead of

layout :admin

since passing a symbol does something different.

class UsersController < ApplicationController
  layout 'admin'
end

So lets say you wanted to only apply layout to specific actions maybe you think you could do this because you can do this with before_actions:

class UsersController < ApplicationController
  layout 'form', only: %w{new create edit update}
end

This, however, doesn't work, so how do you get conditional layouts? This is where symbol comes in, its actually telling it to call a function in the controller to handle the logic

class UsersController < ApplicationController
  layout :guess


  def guess
    case action_name
    when "new", "create", "edit", "update"
      "form"
    else
      "application"
    end
  end

end

Also you noticed when I did this:

%w{new create edit update}

This is a clever shorthand for arrays which will split on the space so its the same as:

['new', 'create' , 'edit' , 'update']

I keep thinking about making an open-source Rails course, can you tell?