4/4/2018 - 3:58 PM


Layouts, partials ...


  • HTML pattern for a page
    • define html elments to be used multiple pages ex.(every page should show a sidebar)
  • Rails provides a default layer layout: views/layouts/application.html.erb
  • Use layout false within a controller to prevent the default layout from being used

Creating a new Layout

  • create a file with extension .html.erb within the layouts
    • in the controller, to use the new layout: layout 'admin'

Setting @page_title

<!--in application layout file-->
<title>Simple CMS Admin | <%= @page_title || "Admin" %></title>

<!--you can then set the `@page_title` in your template files-->
<% @page_title = "Subjects" %>

Partial Templates

  • also known as partials
  • used to place repetitive code inside
  • denoted using _form.html.erb
    • place the repetitive code inside this file
    • replace the code with <%= render(:partial => "form") %>, (no underscore)
    • to pass values in with the partial, need to pass in hash with the values: <%= render(:partial => "form", :locals => {:varinpartial => localvar}) %>
    • Rails will assume the partial is in the same directory, if the partial is not in the same directory, you'll need to supply the full path to the partial(don't need to go too far back, start from the folders within the views directory)