SlideShare a Scribd company logo
1 of 119
DRYing up
Views and Controllers
Layouts, partials, helpers, and ļ¬lters
The Problem
The Problem


I said Rails was big on DRY (donā€™t repeat yourself)
The Problem


I said Rails was big on DRY (donā€™t repeat yourself)
But we are duplicating a lot of code so far!
Add an Article Form
A trivial page containing a form
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Add an Article</title>
  </head>
  <body>
    <h1>Add an Article</h1>
    <% form_for @article do |f| %>
      <%= f.error_messages %>
      <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br>
      <%= f.label :body %><br><%= f.text_area :body %><br>
      <%= f.submit "Post Article" %>
    <% end %>
  </body>
</html>




Add an Article Form
A trivial page containing a form
Update Article Form
Nearly the exact same page
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Update Article</title>
  </head>
  <body>
    <h1>Update Article</h1>
    <% form_for @article do |f| %>
      <%= f.error_messages %>
      <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br>
      <%= f.label :body %><br><%= f.text_area :body %><br>
      <%= f.submit "Save Article" %>
    <% end %>
  </body>
</html>




Update Article Form
Nearly the exact same page
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>Update Article</title>
  </head>
  <body>
    <h1>Update Article</h1>
    <% form_for @article do |f| %>
      <%= f.error_messages %>
      <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br>
      <%= f.label :body %><br><%= f.text_area :body %><br>
      <%= f.submit "Save Article" %>
    <% end %>
  </body>
</html>




Update Article Form
Nearly the exact same page
Solutions
Solutions
 Rails has many different tools to reduce repetition
Solutions
 Rails has many different tools to reduce repetition
   Layouts
Solutions
 Rails has many different tools to reduce repetition
   Layouts
   Partials
Solutions
 Rails has many different tools to reduce repetition
   Layouts
   Partials
   Helpers
Solutions
 Rails has many different tools to reduce repetition
   Layouts
   Partials
   Helpers
   Filters
Solutions
 Rails has many different tools to reduce repetition
   Layouts
   Partials
   Helpers
   Filters
 Letā€™s take a look at what each of these is good for
Layouts
A tool for separating page header
and footer code
Repetitive HTML
Repetitive HTML


Layouts help you to handle header and footer code
Repetitive HTML


Layouts help you to handle header and footer code
  This is handy for HTML <head> ā€¦ </head>
  sections and common site design code
Repetitive HTML


Layouts help you to handle header and footer code
  This is handy for HTML <head> ā€¦ </head>
  sections and common site design code
Rails will render a layout for each page, if available
Layout Selection
                   class ArticlesController <
                       ApplicationController
                    # ...
                   end
Layout Selection
                       class ArticlesController <
Each controller can        ApplicationController
have itā€™s own layout    # ...
                       end
Layout Selection
                           class ArticlesController <
Each controller can            ApplicationController
have itā€™s own layout        # ...
                           end

If a controller doesnā€™t,
Rails will check parent
controllers
Layout Selection
                           class ArticlesController <
Each controller can            ApplicationController
have itā€™s own layout        # ...
                           end

If a controller doesnā€™t,
Rails will check parent
controllers
  application.html.erb
  is the easiest way to
  set a global layout
A Basic Layout
Just yield where you want to insert
the page content
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>My Blog</title>
  </head>
  <body>
    <%= yield %>
  </body>
</html>




A Basic Layout
Just yield where you want to insert
the page content
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>My Blog</title>
  </head>
  <body>
    <%= yield %>
  </body>
</html>




A Basic Layout
Just yield where you want to insert
the page content
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
            "http://www.w3.org/TR/html4/strict.dtd">
<html>
  <head>
    <meta http-equiv="Content-type" content="text/html; charset=utf-8">
    <title>My Blog</title>
  </head>
  <body>
    <%= yield %>
  </body>
</html>




A Basic Layout
Just yield where you want to insert
the page content
The Revised Add Form
This code is inserted into the layout by Rails
to create a full page
<h1>Add an Article</h1>
 <% form_for @article do |f| %>
   <%= f.error_messages %>
   <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br>
   <%= f.label :body %><br><%= f.text_area :body %><br>
   <%= f.submit "Post Article" %>
 <% end %>




The Revised Add Form
This code is inserted into the layout by Rails
to create a full page
The Revised Edit Form
Thereā€™s still some duplication,
but things are deļ¬nitely improving
<h1>Update Article</h1>
 <% form_for @article do |f| %>
   <%= f.error_messages %>
   <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br>
   <%= f.label :body %><br><%= f.text_area :body %><br>
   <%= f.submit "Save Article" %>
 <% end %>




The Revised Edit Form
Thereā€™s still some duplication,
but things are deļ¬nitely improving
Fixing the Title
Fixing the Title

                   <% content_for :name, "Content" %>

                   <% content_for :name do %>
                     <script type="text/javascript"
                          charset="utf-8">
                       // ...
                     </script>
                   <% end %>




                   <%= yield :name %>
Fixing the Title
 content_for() can be
 used to pass content   <% content_for :name, "Content" %>

 between ļ¬les           <% content_for :name do %>
                          <script type="text/javascript"
                               charset="utf-8">
                            // ...
                          </script>
                        <% end %>




                        <%= yield :name %>
Fixing the Title
 content_for() can be
 used to pass content     <% content_for :name, "Content" %>

 between ļ¬les             <% content_for :name do %>
                            <script type="text/javascript"
                                 charset="utf-8">
 One ļ¬le sets content,        // ...
                            </script>
 using a Ruby String or   <% end %>

 a block of HTML
                          <%= yield :name %>
Fixing the Title
 content_for() can be
 used to pass content       <% content_for :name, "Content" %>

 between ļ¬les               <% content_for :name do %>
                              <script type="text/javascript"
                                   charset="utf-8">
 One ļ¬le sets content,          // ...
                              </script>
 using a Ruby String or     <% end %>

 a block of HTML
 Another ļ¬le yields to it   <%= yield :name %>


 by name
Set Title Content
Each page sets relevant title content
<% content_for :page_title, "Add an Article" %>

 <h1>Add an Article</h1>
 <!-- .... -->




                              <% content_for :page_title, "Update Article" %>

                              <h1>Update Article</h1>
                              <!-- ... -->




Set Title Content
Each page sets relevant title content
Read the Title Content
The layout will now make use of the title content
if it exists
<title>
      <%= ["My Blog", yield(:page_title)].compact.join(" : ") %>
    </title>




Read the Title Content
The layout will now make use of the title content
if it exists
Content Sharing in Action
Content Sharing in Action

 We now have dynamic
 titles based on the
 page you are viewing
Content Sharing in Action

 We now have dynamic
 titles based on the
 page you are viewing
Content Sharing in Action

 We now have dynamic
 titles based on the
 page you are viewing
Content Sharing in Action

 We now have dynamic
 titles based on the
 page you are viewing
 content_for() is also
 handy for sidebars and
 other shared content
Partials
A tool for separating repeated
chunks of view code
Duplicate Form Fields
We need to remove more duplication,
but be pragmatic about what to leave
<h1>Update Article</h1>
 <% form_for @article do |f| %>
   <%= f.error_messages %>
   <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br>
   <%= f.label :body %><br><%= f.text_area :body %><br>
   <%= f.submit "Save Article" %>
 <% end %>




Duplicate Form Fields
We need to remove more duplication,
but be pragmatic about what to leave
<h1>Update Article</h1>
 <% form_for @article do |f| %>
   <%= f.error_messages %>
   <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br>
   <%= f.label :body %><br><%= f.text_area :body %><br>
   <%= f.submit "Save Article" %>
 <% end %>




Duplicate Form Fields
We need to remove more duplication,
but be pragmatic about what to leave
<h1>Update Article</h1>
 <% form_for @article do |f| %>
   <%= f.error_messages %>
   <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br>
   <%= f.label :body %><br><%= f.text_area :body %><br>
   <%= f.submit "Save Article" %>
 <% end %>




Duplicate Form Fields
We need to remove more duplication,
but be pragmatic about what to leave
Shared HTML
Shared HTML

Any shared HTML can be placed into a ā€œpartialā€
Shared HTML

Any shared HTML can be placed into a ā€œpartialā€
  This is often used for form ļ¬elds, and code that
  displays the details of an individual model
Shared HTML

Any shared HTML can be placed into a ā€œpartialā€
  This is often used for form ļ¬elds, and code that
  displays the details of an individual model
That partial can then be inserted into all needed places
Shared HTML

Any shared HTML can be placed into a ā€œpartialā€
  This is often used for form ļ¬elds, and code that
  displays the details of an individual model
That partial can then be inserted into all needed places
By convention, partial ļ¬les begin with an _ in Rails (for
example: _article.html.erb)
_form.html.erb
Iā€™ve moved the form ļ¬elds into a separate HTML
ļ¬le, starting with an _ so Rails knows itā€™s a partial
<%= f.error_messages %>
  <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br>
  <%= f.label :body %><br><%= f.text_area :body %><br>




_form.html.erb
Iā€™ve moved the form ļ¬elds into a separate HTML
ļ¬le, starting with an _ so Rails knows itā€™s a partial
<%= f.error_messages %>
  <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br>
  <%= f.label :body %><br><%= f.text_area :body %><br>




_form.html.erb
Iā€™ve moved the form ļ¬elds into a separate HTML
ļ¬le, starting with an _ so Rails knows itā€™s a partial
Forms render() the Partial
We can render() the partial anywhere we need
to reuse it and even pass variables into it
<% content_for :page_title, "Add an Article" %>

 <h1>Add an Article</h1>
 <% form_for @article do |f| %>
   <%= render "form", :f => f %>
   <%= f.submit "Post Article" %>
 <% end %>



                              <% content_for :page_title, "Update Article" %>

                              <h1>Update Article</h1>
                              <% form_for @article do |f| %>
                                <%= render "form", :f => f %>
                                <%= f.submit "Save Article" %>
                              <% end %>




Forms render() the Partial
We can render() the partial anywhere we need
to reuse it and even pass variables into it
<% content_for :page_title, "Add an Article" %>

 <h1>Add an Article</h1>
 <% form_for @article do |f| %>
   <%= render "form", :f => f %>
   <%= f.submit "Post Article" %>
 <% end %>



                              <% content_for :page_title, "Update Article" %>

                              <h1>Update Article</h1>
                              <% form_for @article do |f| %>
                                <%= render "form", :f => f %>
                                <%= f.submit "Save Article" %>
                              <% end %>




Forms render() the Partial
We can render() the partial anywhere we need
to reuse it and even pass variables into it
Partials for Models
Partials for Models

 Rails is smart about partials used to show a model
Partials for Models

 Rails is smart about partials used to show a model
   It can recognize them by name (more conventions!)
Partials for Models

 Rails is smart about partials used to show a model
   It can recognize them by name (more conventions!)
 It will render() the proper partial for a model or
 repeatedly render() the same partial for an entire
 collection of models
Partials for Models

 Rails is smart about partials used to show a model
   It can recognize them by name (more conventions!)
 It will render() the proper partial for a model or
 repeatedly render() the same partial for an entire
 collection of models
 A local variable is set holding the model, again named
 by the type
Manual Iteration
This code works, but Rails is smart enough to
help us if we follow some conventions
<h1>Articles</h1>
<ul>
  <% @articles.each do |article| %>
     <li>
        <%= link_to h(article.title), article_path(article)    %>
        <%= link_to "edit",         edit_article_path(article) %>
     </li>
  <% end %>
</ul>




Manual Iteration
This code works, but Rails is smart enough to
help us if we follow some conventions
_article.html.erb
I moved the Article display code into
an _article.html.erb partial
<li>
    <%= link_to h(article.title), article_path(article)    %>
    <%= link_to "edit",         edit_article_path(article) %>
 </li>




_article.html.erb
I moved the Article display code into
an _article.html.erb partial
<li>
    <%= link_to h(article.title), article_path(article)    %>
    <%= link_to "edit",         edit_article_path(article) %>
 </li>




_article.html.erb
I moved the Article display code into
an _article.html.erb partial
Partial Found by Name
Rails looks for an _article.html.erb to render()
the Article (matching the names)
<h1>Articles</h1>
         <ul>
           <% @articles.each do |article| %>
              <%= render article %>
           <% end %>
         </ul>




Partial Found by Name
Rails looks for an _article.html.erb to render()
the Article (matching the names)
One Step Further
Rails can even recognize a collection (an Array),
render()ing the partial once for each member
<h1>Articles</h1>
              <ul>
                <%= render @articles %>
              </ul>




One Step Further
Rails can even recognize a collection (an Array),
render()ing the partial once for each member
Helpers
A tool for separating out view logic
Where to Hide View Logic
Where to Hide View Logic

Views should be pretty dumb template ļ¬lling code
Where to Hide View Logic

Views should be pretty dumb template ļ¬lling code
Logic in your views is hard to maintain and needs to be
moved
Where to Hide View Logic

Views should be pretty dumb template ļ¬lling code
Logic in your views is hard to maintain and needs to be
moved
  Move business logic into model methods
Where to Hide View Logic

Views should be pretty dumb template ļ¬lling code
Logic in your views is hard to maintain and needs to be
moved
  Move business logic into model methods
  If itā€™s really view logic, write a helper method
Where to Hide View Logic

Views should be pretty dumb template ļ¬lling code
Logic in your views is hard to maintain and needs to be
moved
  Move business logic into model methods
  If itā€™s really view logic, write a helper method
A helper is just a Ruby ā€œMixinā€ Rails adds to the view
These can be Combined
This is some logic though, so it belongs in a
helper method
<% content_for :page_title, "Add an Article" %>

 <h1>Add an Article</h1>
 <% form_for @article do |f| %>
   <%= render "form", :f => f %>
   <%= f.submit "Post Article" %>
 <% end %>



                              <% content_for :page_title, "Update Article" %>

                              <h1>Update Article</h1>
                              <% form_for @article do |f| %>
                                <%= render "form", :f => f %>
                                <%= f.submit "Save Article" %>
                              <% end %>




These can be Combined
This is some logic though, so it belongs in a
helper method
<% content_for :page_title, "Add an Article" %>

 <h1>Add an Article</h1>
 <% form_for @article do |f| %>
   <%= render "form", :f => f %>
   <%= f.submit "Post Article" %>
 <% end %>



                              <% content_for :page_title, "Update Article" %>

                              <h1>Update Article</h1>
                              <% form_for @article do |f| %>
                                <%= render "form", :f => f %>
                                <%= f.submit "Save Article" %>
                              <% end %>




These can be Combined
This is some logic though, so it belongs in a
helper method
Adding a Helper Method
I added this method to the Module (ā€œMixinā€) in
app/helpers/application_helper.rb
module ApplicationHelper
           def page_title(title)
            content_for :page_title, title
            "<h1>#{title}</h1>"
           end
          end




Adding a Helper Method
I added this method to the Module (ā€œMixinā€) in
app/helpers/application_helper.rb
Switch to Using the Helper
The views are a little cleaner now with the logic
moved to the helper
<%= page_title "Add an Article" %>

  <% form_for @article do |f| %>
    <%= render "form", :f => f %>
    <%= f.submit "Post Article" %>
  <% end %>




                                       <%= page_title "Update Article" %>

                                       <% form_for @article do |f| %>
                                         <%= render "form", :f => f %>
                                         <%= f.submit "Save Article" %>
                                       <% end %>




Switch to Using the Helper
The views are a little cleaner now with the logic
moved to the helper
Built-in Helpers
Built-in Helpers
 Rails comes with a ton of helpers, available in all views
Built-in Helpers
 Rails comes with a ton of helpers, available in all views
   Date and time methods
Built-in Helpers
 Rails comes with a ton of helpers, available in all views
   Date and time methods
   Number and currency methods
Built-in Helpers
 Rails comes with a ton of helpers, available in all views
   Date and time methods
   Number and currency methods
   Link and form builders
Built-in Helpers
 Rails comes with a ton of helpers, available in all views
   Date and time methods
   Number and currency methods
   Link and form builders
   Image, CSS, and JavaScript support methods
Built-in Helpers
 Rails comes with a ton of helpers, available in all views
   Date and time methods
   Number and currency methods
   Link and form builders
   Image, CSS, and JavaScript support methods
   ā€¦
An Article Show Page
Uses helpers to escape HTML, show time, and
add simple formatting (like paragraphs) here
<%= page_title h(@article.title) %>

 <p>posted <%= time_ago_in_words @article.created_at %> ago</p>

 <%= simple_format @article.body %>




An Article Show Page
Uses helpers to escape HTML, show time, and
add simple formatting (like paragraphs) here
<%= page_title h(@article.title) %>

 <p>posted <%= time_ago_in_words @article.created_at %> ago</p>

 <%= simple_format @article.body %>




An Article Show Page
Uses helpers to escape HTML, show time, and
add simple formatting (like paragraphs) here
<%= page_title h(@article.title) %>

 <p>posted <%= time_ago_in_words @article.created_at %> ago</p>

 <%= simple_format @article.body %>




An Article Show Page
Uses helpers to escape HTML, show time, and
add simple formatting (like paragraphs) here
<%= page_title h(@article.title) %>

 <p>posted <%= time_ago_in_words @article.created_at %> ago</p>

 <%= simple_format @article.body %>




An Article Show Page
Uses helpers to escape HTML, show time, and
add simple formatting (like paragraphs) here
Filters
A tool for separating repeated
chunks of controller code
Controller Duplication
Itā€™s very common for show, edit, update, and
destroy to start with the same lookup code
class ArticlesController < ApplicationController
           # ...

           def show
            @article = Article.ļ¬nd(params[:id])
           end

           def edit
            @article = Article.ļ¬nd(params[:id])
           end

           def update
            @article = Article.ļ¬nd(params[:id])
            # ...
           end

           def destroy
            @article = Article.ļ¬nd(params[:id])
            # ...
           end
          end




Controller Duplication
Itā€™s very common for show, edit, update, and
destroy to start with the same lookup code
class ArticlesController < ApplicationController
           # ...

           def show
            @article = Article.ļ¬nd(params[:id])
           end

           def edit
            @article = Article.ļ¬nd(params[:id])
           end

           def update
            @article = Article.ļ¬nd(params[:id])
            # ...
           end

           def destroy
            @article = Article.ļ¬nd(params[:id])
            # ...
           end
          end




Controller Duplication
Itā€™s very common for show, edit, update, and
destroy to start with the same lookup code
Before or After an Action
Before or After an Action

 Rails has ļ¬lters that can be run before or after an action
Before or After an Action

 Rails has ļ¬lters that can be run before or after an action
 before_ļ¬lter() is often used to lookup model instances
 or check access control
Before or After an Action

 Rails has ļ¬lters that can be run before or after an action
 before_ļ¬lter() is often used to lookup model instances
 or check access control
   You can choose to skip the action that follows
Before or After an Action

 Rails has ļ¬lters that can be run before or after an action
 before_ļ¬lter() is often used to lookup model instances
 or check access control
   You can choose to skip the action that follows
 after_ļ¬lter() isnā€™t used as much, but it can be handy
 for statistics tracking
Using a before_ļ¬lter()
You can specify a method to call before
certain actions are run
class ArticlesController < ApplicationController
        before_ļ¬lter :ļ¬nd_article, :only => %w[show edit update destroy]

        # ...

        def show
        end

        def edit
        end

        def update
         # ...
        end

        def destroy
         # ...
        end

        private

        def ļ¬nd_article
         @article = Article.ļ¬nd(params[:id])
        end
       end




Using a before_ļ¬lter()
You can specify a method to call before
certain actions are run
class ArticlesController < ApplicationController
        before_ļ¬lter :ļ¬nd_article, :only => %w[show edit update destroy]

        # ...

        def show
        end

        def edit
        end

        def update
         # ...
        end

        def destroy
         # ...
        end

        private

        def ļ¬nd_article
         @article = Article.ļ¬nd(params[:id])
        end
       end




Using a before_ļ¬lter()
You can specify a method to call before
certain actions are run
class ArticlesController < ApplicationController
        before_ļ¬lter :ļ¬nd_article, :only => %w[show edit update destroy]

        # ...

        def show
        end

        def edit
        end

        def update
         # ...
        end

        def destroy
         # ...
        end

        private

        def ļ¬nd_article
         @article = Article.ļ¬nd(params[:id])
        end
       end




Using a before_ļ¬lter()
You can specify a method to call before
certain actions are run
class ArticlesController < ApplicationController
        before_ļ¬lter :ļ¬nd_article, :only => %w[show edit update destroy]

        # ...

        def show
        end

        def edit
        end

        def update
         # ...
        end

        def destroy
         # ...
        end

        private

        def ļ¬nd_article
         @article = Article.ļ¬nd(params[:id])
        end
       end




Using a before_ļ¬lter()
You can specify a method to call before
certain actions are run
Questions?
DRY up Your Views Lab
Your book has instructions on how to remove
the duplication in your code

More Related Content

What's hot

Abstracting functionality with centralised content
Abstracting functionality with centralised contentAbstracting functionality with centralised content
Abstracting functionality with centralised content
Michael Peacock
Ā 
HTL(Sightly) - All you need to know
HTL(Sightly) - All you need to knowHTL(Sightly) - All you need to know
HTL(Sightly) - All you need to know
Prabhdeep Singh
Ā 
JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
BG Java EE Course
Ā 
AEM - Client Libraries
AEM - Client LibrariesAEM - Client Libraries
AEM - Client Libraries
Prabhdeep Singh
Ā 
GDI Seattle Intermediate HTML and CSS Class 1
GDI Seattle Intermediate HTML and CSS Class 1GDI Seattle Intermediate HTML and CSS Class 1
GDI Seattle Intermediate HTML and CSS Class 1
Heather Rock
Ā 
Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2
RORLAB
Ā 

What's hot (20)

A quick guide to Css and java script
A quick guide to Css and  java scriptA quick guide to Css and  java script
A quick guide to Css and java script
Ā 
Introduction to Html5
Introduction to Html5Introduction to Html5
Introduction to Html5
Ā 
Understanding JSP -Servlets
Understanding JSP -ServletsUnderstanding JSP -Servlets
Understanding JSP -Servlets
Ā 
Html 5 in a big nutshell
Html 5 in a big nutshellHtml 5 in a big nutshell
Html 5 in a big nutshell
Ā 
Abstracting functionality with centralised content
Abstracting functionality with centralised contentAbstracting functionality with centralised content
Abstracting functionality with centralised content
Ā 
HTL(Sightly) - All you need to know
HTL(Sightly) - All you need to knowHTL(Sightly) - All you need to know
HTL(Sightly) - All you need to know
Ā 
Learn html elements and structure cheatsheet codecademy
Learn html  elements and structure cheatsheet   codecademyLearn html  elements and structure cheatsheet   codecademy
Learn html elements and structure cheatsheet codecademy
Ā 
Introduction to Sightly
Introduction to SightlyIntroduction to Sightly
Introduction to Sightly
Ā 
Build and deploy Python Django project
Build and deploy Python Django projectBuild and deploy Python Django project
Build and deploy Python Django project
Ā 
CSS Frameworks
CSS FrameworksCSS Frameworks
CSS Frameworks
Ā 
JavaScript and jQuery Fundamentals
JavaScript and jQuery FundamentalsJavaScript and jQuery Fundamentals
JavaScript and jQuery Fundamentals
Ā 
Web Design Basics
Web Design BasicsWeb Design Basics
Web Design Basics
Ā 
ASP
ASPASP
ASP
Ā 
AEM - Client Libraries
AEM - Client LibrariesAEM - Client Libraries
AEM - Client Libraries
Ā 
GDI Seattle Intermediate HTML and CSS Class 1
GDI Seattle Intermediate HTML and CSS Class 1GDI Seattle Intermediate HTML and CSS Class 1
GDI Seattle Intermediate HTML and CSS Class 1
Ā 
jQuery from the very beginning
jQuery from the very beginningjQuery from the very beginning
jQuery from the very beginning
Ā 
HTML5 - Introduction
HTML5 - IntroductionHTML5 - Introduction
HTML5 - Introduction
Ā 
HTML 5 Basics Part One
HTML 5 Basics Part OneHTML 5 Basics Part One
HTML 5 Basics Part One
Ā 
Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2Action View Form Helpers - 1, Season 2
Action View Form Helpers - 1, Season 2
Ā 
Web engineering and Technology
Web engineering and TechnologyWeb engineering and Technology
Web engineering and Technology
Ā 

Similar to DRYing Up Rails Views and Controllers

Zen codingcheatsheet
Zen codingcheatsheetZen codingcheatsheet
Zen codingcheatsheet
goldenveizer
Ā 
Caracteristicas Basicas De Htlm
Caracteristicas Basicas De HtlmCaracteristicas Basicas De Htlm
Caracteristicas Basicas De Htlm
Maria S Rivera
Ā 

Similar to DRYing Up Rails Views and Controllers (20)

Html tutorial
Html tutorialHtml tutorial
Html tutorial
Ā 
HTML CSS and Web Development
HTML CSS and Web DevelopmentHTML CSS and Web Development
HTML CSS and Web Development
Ā 
Django Templates
Django TemplatesDjango Templates
Django Templates
Ā 
HTML Web design english & sinhala mix note
HTML Web design english & sinhala mix noteHTML Web design english & sinhala mix note
HTML Web design english & sinhala mix note
Ā 
html for beginners
html for beginnershtml for beginners
html for beginners
Ā 
Lecture1and2
Lecture1and2Lecture1and2
Lecture1and2
Ā 
The complete-html-cheat-sheet
The complete-html-cheat-sheetThe complete-html-cheat-sheet
The complete-html-cheat-sheet
Ā 
The complete-html-cheat-sheet
The complete-html-cheat-sheetThe complete-html-cheat-sheet
The complete-html-cheat-sheet
Ā 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
Ā 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
Ā 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
Ā 
The Django Web Application Framework 2
The Django Web Application Framework 2The Django Web Application Framework 2
The Django Web Application Framework 2
Ā 
Html
HtmlHtml
Html
Ā 
Html basics
Html basicsHtml basics
Html basics
Ā 
å¼•ćå‡ŗ恗ćØ恗恦恮Django - SoozyCon7
å¼•ćå‡ŗ恗ćØ恗恦恮Django - SoozyCon7å¼•ćå‡ŗ恗ćØ恗恦恮Django - SoozyCon7
å¼•ćå‡ŗ恗ćØ恗恦恮Django - SoozyCon7
Ā 
Zen codingcheatsheet
Zen codingcheatsheetZen codingcheatsheet
Zen codingcheatsheet
Ā 
Hows Haml?
Hows Haml?Hows Haml?
Hows Haml?
Ā 
Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel Rails Antipatterns | Open Session with Chad Pytel
Rails Antipatterns | Open Session with Chad Pytel
Ā 
Caracteristicas Basicas De Htlm
Caracteristicas Basicas De HtlmCaracteristicas Basicas De Htlm
Caracteristicas Basicas De Htlm
Ā 
Geek Moot '09 -- Smarty 101
Geek Moot '09 -- Smarty 101Geek Moot '09 -- Smarty 101
Geek Moot '09 -- Smarty 101
Ā 

More from James Gray

A Dickens of A Keynote
A Dickens of A KeynoteA Dickens of A Keynote
A Dickens of A Keynote
James Gray
Ā 

More from James Gray (18)

A Dickens of A Keynote
A Dickens of A KeynoteA Dickens of A Keynote
A Dickens of A Keynote
Ā 
I Doubt That!
I Doubt That!I Doubt That!
I Doubt That!
Ā 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
Ā 
Counting on God
Counting on GodCounting on God
Counting on God
Ā 
In the Back of Your Mind
In the Back of Your MindIn the Back of Your Mind
In the Back of Your Mind
Ā 
Unblocked
UnblockedUnblocked
Unblocked
Ā 
Module Magic
Module MagicModule Magic
Module Magic
Ā 
API Design
API DesignAPI Design
API Design
Ā 
Amazon's Simple Storage Service (S3)
Amazon's Simple Storage Service (S3)Amazon's Simple Storage Service (S3)
Amazon's Simple Storage Service (S3)
Ā 
Git and GitHub
Git and GitHubGit and GitHub
Git and GitHub
Ā 
Test Coverage in Rails
Test Coverage in RailsTest Coverage in Rails
Test Coverage in Rails
Ā 
Rails Routing And Rendering
Rails Routing And RenderingRails Routing And Rendering
Rails Routing And Rendering
Ā 
Sending Email with Rails
Sending Email with RailsSending Email with Rails
Sending Email with Rails
Ā 
Associations in Rails
Associations in RailsAssociations in Rails
Associations in Rails
Ā 
Building a Rails Interface
Building a Rails InterfaceBuilding a Rails Interface
Building a Rails Interface
Ā 
Rails Model Basics
Rails Model BasicsRails Model Basics
Rails Model Basics
Ā 
Ruby
RubyRuby
Ruby
Ā 
Wed Development on Rails
Wed Development on RailsWed Development on Rails
Wed Development on Rails
Ā 

Recently uploaded

Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Joaquim Jorge
Ā 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
vu2urc
Ā 

Recently uploaded (20)

Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
Ā 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
Ā 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
Ā 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
Ā 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
Ā 
šŸ¬ The future of MySQL is Postgres šŸ˜
šŸ¬  The future of MySQL is Postgres   šŸ˜šŸ¬  The future of MySQL is Postgres   šŸ˜
šŸ¬ The future of MySQL is Postgres šŸ˜
Ā 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Ā 
Finology Group ā€“ Insurtech Innovation Award 2024
Finology Group ā€“ Insurtech Innovation Award 2024Finology Group ā€“ Insurtech Innovation Award 2024
Finology Group ā€“ Insurtech Innovation Award 2024
Ā 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
Ā 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Ā 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Ā 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
Ā 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
Ā 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
Ā 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
Ā 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
Ā 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
Ā 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
Ā 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
Ā 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
Ā 

DRYing Up Rails Views and Controllers

  • 1. DRYing up Views and Controllers Layouts, partials, helpers, and ļ¬lters
  • 3. The Problem I said Rails was big on DRY (donā€™t repeat yourself)
  • 4. The Problem I said Rails was big on DRY (donā€™t repeat yourself) But we are duplicating a lot of code so far!
  • 5. Add an Article Form A trivial page containing a form
  • 6. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>Add an Article</title> </head> <body> <h1>Add an Article</h1> <% form_for @article do |f| %> <%= f.error_messages %> <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br> <%= f.label :body %><br><%= f.text_area :body %><br> <%= f.submit "Post Article" %> <% end %> </body> </html> Add an Article Form A trivial page containing a form
  • 7. Update Article Form Nearly the exact same page
  • 8. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>Update Article</title> </head> <body> <h1>Update Article</h1> <% form_for @article do |f| %> <%= f.error_messages %> <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br> <%= f.label :body %><br><%= f.text_area :body %><br> <%= f.submit "Save Article" %> <% end %> </body> </html> Update Article Form Nearly the exact same page
  • 9. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>Update Article</title> </head> <body> <h1>Update Article</h1> <% form_for @article do |f| %> <%= f.error_messages %> <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br> <%= f.label :body %><br><%= f.text_area :body %><br> <%= f.submit "Save Article" %> <% end %> </body> </html> Update Article Form Nearly the exact same page
  • 11. Solutions Rails has many different tools to reduce repetition
  • 12. Solutions Rails has many different tools to reduce repetition Layouts
  • 13. Solutions Rails has many different tools to reduce repetition Layouts Partials
  • 14. Solutions Rails has many different tools to reduce repetition Layouts Partials Helpers
  • 15. Solutions Rails has many different tools to reduce repetition Layouts Partials Helpers Filters
  • 16. Solutions Rails has many different tools to reduce repetition Layouts Partials Helpers Filters Letā€™s take a look at what each of these is good for
  • 17. Layouts A tool for separating page header and footer code
  • 19. Repetitive HTML Layouts help you to handle header and footer code
  • 20. Repetitive HTML Layouts help you to handle header and footer code This is handy for HTML <head> ā€¦ </head> sections and common site design code
  • 21. Repetitive HTML Layouts help you to handle header and footer code This is handy for HTML <head> ā€¦ </head> sections and common site design code Rails will render a layout for each page, if available
  • 22. Layout Selection class ArticlesController < ApplicationController # ... end
  • 23. Layout Selection class ArticlesController < Each controller can ApplicationController have itā€™s own layout # ... end
  • 24. Layout Selection class ArticlesController < Each controller can ApplicationController have itā€™s own layout # ... end If a controller doesnā€™t, Rails will check parent controllers
  • 25. Layout Selection class ArticlesController < Each controller can ApplicationController have itā€™s own layout # ... end If a controller doesnā€™t, Rails will check parent controllers application.html.erb is the easiest way to set a global layout
  • 26. A Basic Layout Just yield where you want to insert the page content
  • 27. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>My Blog</title> </head> <body> <%= yield %> </body> </html> A Basic Layout Just yield where you want to insert the page content
  • 28. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>My Blog</title> </head> <body> <%= yield %> </body> </html> A Basic Layout Just yield where you want to insert the page content
  • 29. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> <html> <head> <meta http-equiv="Content-type" content="text/html; charset=utf-8"> <title>My Blog</title> </head> <body> <%= yield %> </body> </html> A Basic Layout Just yield where you want to insert the page content
  • 30. The Revised Add Form This code is inserted into the layout by Rails to create a full page
  • 31. <h1>Add an Article</h1> <% form_for @article do |f| %> <%= f.error_messages %> <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br> <%= f.label :body %><br><%= f.text_area :body %><br> <%= f.submit "Post Article" %> <% end %> The Revised Add Form This code is inserted into the layout by Rails to create a full page
  • 32. The Revised Edit Form Thereā€™s still some duplication, but things are deļ¬nitely improving
  • 33. <h1>Update Article</h1> <% form_for @article do |f| %> <%= f.error_messages %> <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br> <%= f.label :body %><br><%= f.text_area :body %><br> <%= f.submit "Save Article" %> <% end %> The Revised Edit Form Thereā€™s still some duplication, but things are deļ¬nitely improving
  • 35. Fixing the Title <% content_for :name, "Content" %> <% content_for :name do %> <script type="text/javascript" charset="utf-8"> // ... </script> <% end %> <%= yield :name %>
  • 36. Fixing the Title content_for() can be used to pass content <% content_for :name, "Content" %> between ļ¬les <% content_for :name do %> <script type="text/javascript" charset="utf-8"> // ... </script> <% end %> <%= yield :name %>
  • 37. Fixing the Title content_for() can be used to pass content <% content_for :name, "Content" %> between ļ¬les <% content_for :name do %> <script type="text/javascript" charset="utf-8"> One ļ¬le sets content, // ... </script> using a Ruby String or <% end %> a block of HTML <%= yield :name %>
  • 38. Fixing the Title content_for() can be used to pass content <% content_for :name, "Content" %> between ļ¬les <% content_for :name do %> <script type="text/javascript" charset="utf-8"> One ļ¬le sets content, // ... </script> using a Ruby String or <% end %> a block of HTML Another ļ¬le yields to it <%= yield :name %> by name
  • 39. Set Title Content Each page sets relevant title content
  • 40. <% content_for :page_title, "Add an Article" %> <h1>Add an Article</h1> <!-- .... --> <% content_for :page_title, "Update Article" %> <h1>Update Article</h1> <!-- ... --> Set Title Content Each page sets relevant title content
  • 41. Read the Title Content The layout will now make use of the title content if it exists
  • 42. <title> <%= ["My Blog", yield(:page_title)].compact.join(" : ") %> </title> Read the Title Content The layout will now make use of the title content if it exists
  • 44. Content Sharing in Action We now have dynamic titles based on the page you are viewing
  • 45. Content Sharing in Action We now have dynamic titles based on the page you are viewing
  • 46. Content Sharing in Action We now have dynamic titles based on the page you are viewing
  • 47. Content Sharing in Action We now have dynamic titles based on the page you are viewing content_for() is also handy for sidebars and other shared content
  • 48. Partials A tool for separating repeated chunks of view code
  • 49. Duplicate Form Fields We need to remove more duplication, but be pragmatic about what to leave
  • 50. <h1>Update Article</h1> <% form_for @article do |f| %> <%= f.error_messages %> <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br> <%= f.label :body %><br><%= f.text_area :body %><br> <%= f.submit "Save Article" %> <% end %> Duplicate Form Fields We need to remove more duplication, but be pragmatic about what to leave
  • 51. <h1>Update Article</h1> <% form_for @article do |f| %> <%= f.error_messages %> <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br> <%= f.label :body %><br><%= f.text_area :body %><br> <%= f.submit "Save Article" %> <% end %> Duplicate Form Fields We need to remove more duplication, but be pragmatic about what to leave
  • 52. <h1>Update Article</h1> <% form_for @article do |f| %> <%= f.error_messages %> <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br> <%= f.label :body %><br><%= f.text_area :body %><br> <%= f.submit "Save Article" %> <% end %> Duplicate Form Fields We need to remove more duplication, but be pragmatic about what to leave
  • 54. Shared HTML Any shared HTML can be placed into a ā€œpartialā€
  • 55. Shared HTML Any shared HTML can be placed into a ā€œpartialā€ This is often used for form ļ¬elds, and code that displays the details of an individual model
  • 56. Shared HTML Any shared HTML can be placed into a ā€œpartialā€ This is often used for form ļ¬elds, and code that displays the details of an individual model That partial can then be inserted into all needed places
  • 57. Shared HTML Any shared HTML can be placed into a ā€œpartialā€ This is often used for form ļ¬elds, and code that displays the details of an individual model That partial can then be inserted into all needed places By convention, partial ļ¬les begin with an _ in Rails (for example: _article.html.erb)
  • 58. _form.html.erb Iā€™ve moved the form ļ¬elds into a separate HTML ļ¬le, starting with an _ so Rails knows itā€™s a partial
  • 59. <%= f.error_messages %> <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br> <%= f.label :body %><br><%= f.text_area :body %><br> _form.html.erb Iā€™ve moved the form ļ¬elds into a separate HTML ļ¬le, starting with an _ so Rails knows itā€™s a partial
  • 60. <%= f.error_messages %> <%= f.label :title %><br><%= f.text_ļ¬eld :title %><br> <%= f.label :body %><br><%= f.text_area :body %><br> _form.html.erb Iā€™ve moved the form ļ¬elds into a separate HTML ļ¬le, starting with an _ so Rails knows itā€™s a partial
  • 61. Forms render() the Partial We can render() the partial anywhere we need to reuse it and even pass variables into it
  • 62. <% content_for :page_title, "Add an Article" %> <h1>Add an Article</h1> <% form_for @article do |f| %> <%= render "form", :f => f %> <%= f.submit "Post Article" %> <% end %> <% content_for :page_title, "Update Article" %> <h1>Update Article</h1> <% form_for @article do |f| %> <%= render "form", :f => f %> <%= f.submit "Save Article" %> <% end %> Forms render() the Partial We can render() the partial anywhere we need to reuse it and even pass variables into it
  • 63. <% content_for :page_title, "Add an Article" %> <h1>Add an Article</h1> <% form_for @article do |f| %> <%= render "form", :f => f %> <%= f.submit "Post Article" %> <% end %> <% content_for :page_title, "Update Article" %> <h1>Update Article</h1> <% form_for @article do |f| %> <%= render "form", :f => f %> <%= f.submit "Save Article" %> <% end %> Forms render() the Partial We can render() the partial anywhere we need to reuse it and even pass variables into it
  • 65. Partials for Models Rails is smart about partials used to show a model
  • 66. Partials for Models Rails is smart about partials used to show a model It can recognize them by name (more conventions!)
  • 67. Partials for Models Rails is smart about partials used to show a model It can recognize them by name (more conventions!) It will render() the proper partial for a model or repeatedly render() the same partial for an entire collection of models
  • 68. Partials for Models Rails is smart about partials used to show a model It can recognize them by name (more conventions!) It will render() the proper partial for a model or repeatedly render() the same partial for an entire collection of models A local variable is set holding the model, again named by the type
  • 69. Manual Iteration This code works, but Rails is smart enough to help us if we follow some conventions
  • 70. <h1>Articles</h1> <ul> <% @articles.each do |article| %> <li> <%= link_to h(article.title), article_path(article) %> <%= link_to "edit", edit_article_path(article) %> </li> <% end %> </ul> Manual Iteration This code works, but Rails is smart enough to help us if we follow some conventions
  • 71. _article.html.erb I moved the Article display code into an _article.html.erb partial
  • 72. <li> <%= link_to h(article.title), article_path(article) %> <%= link_to "edit", edit_article_path(article) %> </li> _article.html.erb I moved the Article display code into an _article.html.erb partial
  • 73. <li> <%= link_to h(article.title), article_path(article) %> <%= link_to "edit", edit_article_path(article) %> </li> _article.html.erb I moved the Article display code into an _article.html.erb partial
  • 74. Partial Found by Name Rails looks for an _article.html.erb to render() the Article (matching the names)
  • 75. <h1>Articles</h1> <ul> <% @articles.each do |article| %> <%= render article %> <% end %> </ul> Partial Found by Name Rails looks for an _article.html.erb to render() the Article (matching the names)
  • 76. One Step Further Rails can even recognize a collection (an Array), render()ing the partial once for each member
  • 77. <h1>Articles</h1> <ul> <%= render @articles %> </ul> One Step Further Rails can even recognize a collection (an Array), render()ing the partial once for each member
  • 78. Helpers A tool for separating out view logic
  • 79. Where to Hide View Logic
  • 80. Where to Hide View Logic Views should be pretty dumb template ļ¬lling code
  • 81. Where to Hide View Logic Views should be pretty dumb template ļ¬lling code Logic in your views is hard to maintain and needs to be moved
  • 82. Where to Hide View Logic Views should be pretty dumb template ļ¬lling code Logic in your views is hard to maintain and needs to be moved Move business logic into model methods
  • 83. Where to Hide View Logic Views should be pretty dumb template ļ¬lling code Logic in your views is hard to maintain and needs to be moved Move business logic into model methods If itā€™s really view logic, write a helper method
  • 84. Where to Hide View Logic Views should be pretty dumb template ļ¬lling code Logic in your views is hard to maintain and needs to be moved Move business logic into model methods If itā€™s really view logic, write a helper method A helper is just a Ruby ā€œMixinā€ Rails adds to the view
  • 85. These can be Combined This is some logic though, so it belongs in a helper method
  • 86. <% content_for :page_title, "Add an Article" %> <h1>Add an Article</h1> <% form_for @article do |f| %> <%= render "form", :f => f %> <%= f.submit "Post Article" %> <% end %> <% content_for :page_title, "Update Article" %> <h1>Update Article</h1> <% form_for @article do |f| %> <%= render "form", :f => f %> <%= f.submit "Save Article" %> <% end %> These can be Combined This is some logic though, so it belongs in a helper method
  • 87. <% content_for :page_title, "Add an Article" %> <h1>Add an Article</h1> <% form_for @article do |f| %> <%= render "form", :f => f %> <%= f.submit "Post Article" %> <% end %> <% content_for :page_title, "Update Article" %> <h1>Update Article</h1> <% form_for @article do |f| %> <%= render "form", :f => f %> <%= f.submit "Save Article" %> <% end %> These can be Combined This is some logic though, so it belongs in a helper method
  • 88. Adding a Helper Method I added this method to the Module (ā€œMixinā€) in app/helpers/application_helper.rb
  • 89. module ApplicationHelper def page_title(title) content_for :page_title, title "<h1>#{title}</h1>" end end Adding a Helper Method I added this method to the Module (ā€œMixinā€) in app/helpers/application_helper.rb
  • 90. Switch to Using the Helper The views are a little cleaner now with the logic moved to the helper
  • 91. <%= page_title "Add an Article" %> <% form_for @article do |f| %> <%= render "form", :f => f %> <%= f.submit "Post Article" %> <% end %> <%= page_title "Update Article" %> <% form_for @article do |f| %> <%= render "form", :f => f %> <%= f.submit "Save Article" %> <% end %> Switch to Using the Helper The views are a little cleaner now with the logic moved to the helper
  • 93. Built-in Helpers Rails comes with a ton of helpers, available in all views
  • 94. Built-in Helpers Rails comes with a ton of helpers, available in all views Date and time methods
  • 95. Built-in Helpers Rails comes with a ton of helpers, available in all views Date and time methods Number and currency methods
  • 96. Built-in Helpers Rails comes with a ton of helpers, available in all views Date and time methods Number and currency methods Link and form builders
  • 97. Built-in Helpers Rails comes with a ton of helpers, available in all views Date and time methods Number and currency methods Link and form builders Image, CSS, and JavaScript support methods
  • 98. Built-in Helpers Rails comes with a ton of helpers, available in all views Date and time methods Number and currency methods Link and form builders Image, CSS, and JavaScript support methods ā€¦
  • 99. An Article Show Page Uses helpers to escape HTML, show time, and add simple formatting (like paragraphs) here
  • 100. <%= page_title h(@article.title) %> <p>posted <%= time_ago_in_words @article.created_at %> ago</p> <%= simple_format @article.body %> An Article Show Page Uses helpers to escape HTML, show time, and add simple formatting (like paragraphs) here
  • 101. <%= page_title h(@article.title) %> <p>posted <%= time_ago_in_words @article.created_at %> ago</p> <%= simple_format @article.body %> An Article Show Page Uses helpers to escape HTML, show time, and add simple formatting (like paragraphs) here
  • 102. <%= page_title h(@article.title) %> <p>posted <%= time_ago_in_words @article.created_at %> ago</p> <%= simple_format @article.body %> An Article Show Page Uses helpers to escape HTML, show time, and add simple formatting (like paragraphs) here
  • 103. <%= page_title h(@article.title) %> <p>posted <%= time_ago_in_words @article.created_at %> ago</p> <%= simple_format @article.body %> An Article Show Page Uses helpers to escape HTML, show time, and add simple formatting (like paragraphs) here
  • 104. Filters A tool for separating repeated chunks of controller code
  • 105. Controller Duplication Itā€™s very common for show, edit, update, and destroy to start with the same lookup code
  • 106. class ArticlesController < ApplicationController # ... def show @article = Article.ļ¬nd(params[:id]) end def edit @article = Article.ļ¬nd(params[:id]) end def update @article = Article.ļ¬nd(params[:id]) # ... end def destroy @article = Article.ļ¬nd(params[:id]) # ... end end Controller Duplication Itā€™s very common for show, edit, update, and destroy to start with the same lookup code
  • 107. class ArticlesController < ApplicationController # ... def show @article = Article.ļ¬nd(params[:id]) end def edit @article = Article.ļ¬nd(params[:id]) end def update @article = Article.ļ¬nd(params[:id]) # ... end def destroy @article = Article.ļ¬nd(params[:id]) # ... end end Controller Duplication Itā€™s very common for show, edit, update, and destroy to start with the same lookup code
  • 108. Before or After an Action
  • 109. Before or After an Action Rails has ļ¬lters that can be run before or after an action
  • 110. Before or After an Action Rails has ļ¬lters that can be run before or after an action before_ļ¬lter() is often used to lookup model instances or check access control
  • 111. Before or After an Action Rails has ļ¬lters that can be run before or after an action before_ļ¬lter() is often used to lookup model instances or check access control You can choose to skip the action that follows
  • 112. Before or After an Action Rails has ļ¬lters that can be run before or after an action before_ļ¬lter() is often used to lookup model instances or check access control You can choose to skip the action that follows after_ļ¬lter() isnā€™t used as much, but it can be handy for statistics tracking
  • 113. Using a before_ļ¬lter() You can specify a method to call before certain actions are run
  • 114. class ArticlesController < ApplicationController before_ļ¬lter :ļ¬nd_article, :only => %w[show edit update destroy] # ... def show end def edit end def update # ... end def destroy # ... end private def ļ¬nd_article @article = Article.ļ¬nd(params[:id]) end end Using a before_ļ¬lter() You can specify a method to call before certain actions are run
  • 115. class ArticlesController < ApplicationController before_ļ¬lter :ļ¬nd_article, :only => %w[show edit update destroy] # ... def show end def edit end def update # ... end def destroy # ... end private def ļ¬nd_article @article = Article.ļ¬nd(params[:id]) end end Using a before_ļ¬lter() You can specify a method to call before certain actions are run
  • 116. class ArticlesController < ApplicationController before_ļ¬lter :ļ¬nd_article, :only => %w[show edit update destroy] # ... def show end def edit end def update # ... end def destroy # ... end private def ļ¬nd_article @article = Article.ļ¬nd(params[:id]) end end Using a before_ļ¬lter() You can specify a method to call before certain actions are run
  • 117. class ArticlesController < ApplicationController before_ļ¬lter :ļ¬nd_article, :only => %w[show edit update destroy] # ... def show end def edit end def update # ... end def destroy # ... end private def ļ¬nd_article @article = Article.ļ¬nd(params[:id]) end end Using a before_ļ¬lter() You can specify a method to call before certain actions are run
  • 119. DRY up Your Views Lab Your book has instructions on how to remove the duplication in your code

Editor's Notes