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