SlideShare ist ein Scribd-Unternehmen logo
1 von 49
Building the User Interface
The C and V of MCV
What is a Controller?

 A controller is the switchboard operator between your
 views, or web interface, and the model, or database
 Rails convention is “Fat models, skinny controllers.”
 7 basic actions will accomplish 90% of desired
 behavior
Generate a Controller

 generate/script controller controller_name
 Optionally, you can add action names and the
 controller will generate with those methods and view
 files
 Controllers are usually plural
 Generating the controller also creates the view directory
Basic Controller Actions
                                       class PetsController < ApplicationController
 index() - list of like objects
                                         def index
                                         end
 new() - displays a new form
                                         def new
                                         end
 create() - saves new form
                                         def create
 input to DB                             end

                                         def edit
 edit() - displays an edit form          end

                                         def update
 update() - saves edit form input to     end
 DB
                                         def show
                                         end
 show() - displays single object
                                         def destroy
                                         end
 destroy() - deletes single object
                                       end
Building the new() action

                    def new
                      @pet = Pet.new
                    end
Building the new() action
 new() asks the
 browser to display a   def new
                          @pet = Pet.new
 form                   end
Building the new() action
 new() asks the
 browser to display a   def new
                          @pet = Pet.new
 form                   end

 Simply provide an
 empty object
Building the new() action
 new() asks the
 browser to display a      def new
                             @pet = Pet.new
 form                      end

 Simply provide an
 empty object
 By rails convention, an
 action name and a
 view name should be
 the same
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Pets Are People Too!</title>
  </head>
  <body>
    <% form_for @pet do |f| %>
      <%= f.error_messages %>
      <p><%= f.label :name %><br /><%= f.text_field :name %></p>
      <p><%= f.label :animal_type %><br /><%= f.text_field :animal_type %></p>
      <p><%= f.label :breed %><br /><%= f.text_field :breed %></p>
      <%= f.submit 'Create' %>
    <% end %>
  </body>
</html>




The new.html.erb form
Rails uses ERB to gain access to Ruby code
inside your HTML views
Meet ERB

Using ERB tags allows you to embed Ruby code
directly in your HTML code
The <% ... %> is used for block code (iterators and
forms) or code you don’t want inserted
The <%= ... %> inserts whatever is inside the tag
The <%# ... %> comments out the tag contents
A Closer Look
                <% form_for @pet do |f| %>
                  <%= f.error_messages %>
                  <p>
                      <%= f.label :name %><br />
                      <%= f.text_field :name %>
                  </p>
                  <p>
                      <%= f.label :animal_type %><br />
                      <%= f.text_field :animal_type %>
                  </p>
                  <p>
                      <%= f.label :breed %><br />
                      <%= f.text_field :breed %>
                  </p>
                  <%= f.submit 'Create' %>
                <% end %>
A Closer Look
The first line sets up
the code to follow      <% form_for @pet do |f| %>
                          <%= f.error_messages %>
                          <p>
                              <%= f.label :name %><br />
                              <%= f.text_field :name %>
                          </p>
                          <p>
                              <%= f.label :animal_type %><br />
                              <%= f.text_field :animal_type %>
                          </p>
                          <p>
                              <%= f.label :breed %><br />
                              <%= f.text_field :breed %>
                          </p>
                          <%= f.submit 'Create' %>
                        <% end %>
A Closer Look
The first line sets up
the code to follow      <% form_for @pet do |f| %>
                          <%= f.error_messages %>
                          <p>
                              <%= f.label :name %><br />
                              <%= f.text_field :name %>
                          </p>
                          <p>
                              <%= f.label :animal_type %><br />
                              <%= f.text_field :animal_type %>
                          </p>
                          <p>
                              <%= f.label :breed %><br />
                              <%= f.text_field :breed %>
                          </p>
                          <%= f.submit 'Create' %>
                        <% end %>
A Closer Look
The first line sets up
the code to follow        <% form_for @pet do |f| %>
                            <%= f.error_messages %>
                            <p>
                                <%= f.label :name %><br />
Inside the <p> tags are         <%= f.text_field :name %>
                            </p>
displayed pieces of         <p>
                                <%= f.label :animal_type %><br />
code                            <%= f.text_field :animal_type %>
                            </p>
                            <p>
                                <%= f.label :breed %><br />
                                <%= f.text_field :breed %>
                            </p>
                            <%= f.submit 'Create' %>
                          <% end %>
A Closer Look
The first line sets up
the code to follow        <% form_for @pet do |f| %>
                            <%= f.error_messages %>
                            <p>
                                <%= f.label :name %><br />
Inside the <p> tags are         <%= f.text_field :name %>
                            </p>
displayed pieces of         <p>
                                <%= f.label :animal_type %><br />
code                            <%= f.text_field :animal_type %>
                            </p>
                            <p>
                                <%= f.label :breed %><br />
                                <%= f.text_field :breed %>
                            </p>
                            <%= f.submit 'Create' %>
                          <% end %>
A Closer Look
The first line sets up
the code to follow        <% form_for @pet do |f| %>
                            <%= f.error_messages %>
                            <p>
                                <%= f.label :name %><br />
Inside the <p> tags are         <%= f.text_field :name %>
                            </p>
displayed pieces of         <p>
                                <%= f.label :animal_type %><br />
code                            <%= f.text_field :animal_type %>
                            </p>
                            <p>
Rails provides lots of          <%= f.label :breed %><br />
                                <%= f.text_field :breed %>
helpers so you don’t        </p>
                            <%= f.submit 'Create' %>
have to spend time        <% end %>

writing lots of HTML
A Closer Look
The first line sets up
the code to follow        <% form_for @pet do |f| %>
                            <%= f.error_messages %>
                            <p>
                                <%= f.label :name %><br />
Inside the <p> tags are         <%= f.text_field :name %>
                            </p>
displayed pieces of         <p>
                                <%= f.label :animal_type %><br />
code                            <%= f.text_field :animal_type %>
                            </p>
                            <p>
Rails provides lots of          <%= f.label :breed %><br />
                                <%= f.text_field :breed %>
helpers so you don’t        </p>
                            <%= f.submit 'Create' %>
have to spend time        <% end %>

writing lots of HTML
Why did I get an error?
You haven’t told Rails what the path to your form
is yet.
Why did I get an error?
You haven’t told Rails what the path to your form
is yet.
Why did I get an error?
You haven’t told Rails what the path to your form
is yet.
Why did I get an error?
You haven’t told Rails what the path to your form
is yet.
Routing

Set routes in the
config/routes.rb file
                           ActionController::Routing::Routes.draw do |map|
Provide the resource        map.resources :pets

name and Rails will          map.connect ':controller/:action/:id'

create the 7 routes          map.connect ':controller/:action/:id.:format'
                           end



Setting a route requires
a server restart
Displaying the form

 The standard path for a
 resource is the
 controller name
 followed by the action
 We’ve collected the
 info. Now what?
create() the Pet Object

 Create your Pet object     def create
                              @pet = Pet.new(params[:pet])


 Check to see if it saved     if @pet.save
                                flash[:notice] = "Your Pet has
                                     been saved."
 Tell the browser how to        redirect_to new_pet_path

 respond                      else
                                flash.now[:error] = "There was
                                    a problem saving your Pet"
 What are params[] and          render :action => "new"
                              end
 flash[]?                    end
Parameters

Parameters are the pieces of information passed back
to the controller action from the HTML form fields
Rails collects them in a Hash
Using Rails conventions, the Object name is the Hash
name for the params
What the browser sees
Processing PetsController#create (for 127.0.0.1 at 2010-03-07 19:24:57) [POST]
  Parameters: {"commit"=>"Create",
               "pet"=>{"name"=>"Snow Paw", "breed"=>"Snowshoe Siamese", "animal_type"=>"Cat"}}
Redirected to http://localhost:3000/pets
Completed in 19ms (DB: 0) | 302 Found [http://localhost/pets]




What the browser sees
The server request shows the parameters
coming in from the form as a Hash
<% {:notice => "green", :error => "red"}.each do |message, color| %>
  <% next if flash[message].blank? %>
  <div style="color: <%= color %>;">
      <%= flash[message] %>
  </div>
<% end %>




flash[ ] Messages
flash[ ] messages give feedback to the user
Rails automatically remembers the flash between requests
def create
   @pet = Pet.new(params[:pet])

   if @pet.save
     flash[:notice] = "Your
       Pet has been saved."
     redirect_to new_pet_path
   else
     ...
   end
 end




Successful Create
@pet was successfully saved to the database so
a flash message displays and we’re redirected
def create
   @pet = Pet.new(params[:pet])

   if @pet.save
     ...
   else
     flash.now[:error] = "There
      was a problem saving
      your Pet"
     render :action => "new"
   end
 end




Failed Create
@pet failed a validation so the page was re-
rendered with the flash message and errors
Render and Redirect

redirect_to() - redirects the browser to the target
passed to it
render() - unless otherwise indicated with a
redirect_to() or explicit render() call, the view file
named after the current action is displayed
  If you pass in an action name to render(), it will
  render that content instead
            render :action => "new"
Whew! We made it. Now
let’s check out the remaining
five actions.
index()
index()
index() shows a list or
collection of objects     def index
                            @pets = Pet.all
It sets a variable that   end

contains the collection
desired
index()
index() shows a list or
collection of objects      def index
                             @pets = Pet.all
It sets a variable that    end

contains the collection
desired
                           def index
Generally you want to        @pets = Pet.all(:limit => 20)
                           end
set some kind of
criteria on the find() to
limit large lists
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Pets Are People Too!</title>
  </head>
  <body>
    <table>
         <tr>
              <td>Name</td> <td>Animal Type</td> <td>Breed</td> <td>Actions</td>
         </tr>
        <% @pets.each do |pet| %>
              <tr>
                   <td><%= h pet.name %></td>
                   <td><%= h pet.animal_type %></td>
                   <td><%= h pet.breed %></td>
                   <td><%= link_to "View", pet_path(pet) %></td>
              </tr>
        <% end %>
    </table>
    <%= link_to "Add a New Pet", new_pet_path %>
 </body>
</html>




HTML for an index view
link_to() takes the name of the link and the path
See how I’m iterating through the collection?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                         "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Pets Are People Too!</title>
  </head>
  <body>
    <table>
         <tr>
              <td>Name</td> <td>Animal Type</td> <td>Breed</td> <td>Actions</td>
         </tr>
        <% @pets.each do |pet| %>
              <tr>
                   <td><%= h pet.name %></td>
                   <td><%= h pet.animal_type %></td>
                   <td><%= h pet.breed %></td>
                   <td><%= link_to "View", pet_path(pet) %></td>
              </tr>
        <% end %>
    </table>
    <%= link_to "Add a New Pet", new_pet_path %>
 </body>
</html>




HTML for an index view
link_to() takes the name of the link and the path
See how I’m iterating through the collection?
From the Browser
index() is the default action, so the address bar
only needs the controller name
show()
show() - displays a
single object
Should set the object
to display              def show
                          @pet = Pet.find(params[:id])
                        end
The unique ID for a
specific Pet comes in
as a parameter from
the browser
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                       "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Pets Are People Too!</title>
  </head>
  <body>
    <% {:notice => "green", :error => "red"}.each do |message, color| %>
      <% next if flash[message].blank? %>
      <div style="color: <%= color %>;">
           <%= flash[message] %>
      </div>
    <% end %>
    <p>Name: <%= h @pet.name %></p>
    <p>Animal Type: <%= h @pet.animal_type %></p>
    <p>Breed: <%= h @pet.breed %></p>
    <p><%= link_to "View All", pets_path %> |
        <%= link_to "Edit", edit_pet_path(@pet) %> |
        <%= link_to "Delete", pet_path(@pet), :method => :delete %></p>
 </body>
</html>




HTML for a show view
by passing :method to link_to(), you can specify
an HTTP action of that verb
From the Browser
show() needs an ID, so the address bar needs
the controller name and the object ID
edit() and update()
edit() and update() are
                          def edit
very similar to new()       @pet = Pet.find(params[:id])
                          end
and create()
                          def update
                            @pet = Pet.find(params[:id])
They require the object     if @pet.update_attributes(params[:pet])
ID                            flash[:notice] = "Your Pet has been
                                                updated."
                              redirect_to pets_path

edit() renders the form     else
                              flash.now[:error] = "Something went

while update()                                     wrong."
                              render :action => "edit"

performs the actual
                            end
                          end

update
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
                      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Pets Are People Too!</title>
  </head>
  <body>
    <% form_for @pet do |f| %>
      <%= f.error_messages %>
      <p><%= f.label :name %><br /><%= f.text_field :name %></p>
      <p><%= f.label :animal_type %><br /><%= f.text_field :animal_type %></p>
      <p><%= f.label :breed %><br /><%= f.text_field :breed %></p>
      <%= f.submit 'Update' %>
    <% end %>
  </body>
</html>




HTML for the edit view
When you select the edit() action, the form pulls
in the object data
From the Browser
edit() needs an ID, so the address bar needs the
controller name, the object ID and the action
destroy()
destroy() doesn’t have       def destroy
                               @pet = Pet.find(params[:id])

a view                         @pet.destroy
                               flash[:notice] = "The Pet was deleted."
                               redirect_to pets_path

Passing a :method
                             end



parameter into link_to()
forces the use of the      <p>
passed HTTP verb              <%= link_to "View All", pets_path %> |
                              <%= link_to "Edit",
                                  edit_pet_path(@pet)%> |

Remember the show             <%= link_to "Delete",
                                  pet_path(@pet), :method => :delete %>
                           </p>
HTML code?
From the Browser
Clicking Delete redirects the user back to the
index view. Notice the missing fish?
Questions?
Building an Interface Lab
Your book has instructions on how to create
controllers to show your models

Weitere ähnliche Inhalte

Andere mochten auch

A Dickens of A Keynote
A Dickens of A KeynoteA Dickens of A Keynote
A Dickens of A KeynoteJames Gray
 
Counting on God
Counting on GodCounting on God
Counting on GodJames Gray
 
Regular expressions
Regular expressionsRegular expressions
Regular expressionsJames Gray
 
Techniques for Reviewing a User Interface
Techniques for Reviewing a User InterfaceTechniques for Reviewing a User Interface
Techniques for Reviewing a User InterfaceRhonda Bracey
 
Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)Jargalsaikhan Alyeksandr
 

Andere mochten auch (6)

A Dickens of A Keynote
A Dickens of A KeynoteA Dickens of A Keynote
A Dickens of A Keynote
 
Counting on God
Counting on GodCounting on God
Counting on God
 
I Doubt That!
I Doubt That!I Doubt That!
I Doubt That!
 
Regular expressions
Regular expressionsRegular expressions
Regular expressions
 
Techniques for Reviewing a User Interface
Techniques for Reviewing a User InterfaceTechniques for Reviewing a User Interface
Techniques for Reviewing a User Interface
 
Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)Database design & Normalization (1NF, 2NF, 3NF)
Database design & Normalization (1NF, 2NF, 3NF)
 

Ähnlich wie Building a Rails Interface

Getting started with Rails (4), Season 2
Getting started with Rails (4), Season 2Getting started with Rails (4), Season 2
Getting started with Rails (4), Season 2RORLAB
 
Making Sense of Twig
Making Sense of TwigMaking Sense of Twig
Making Sense of TwigBrandon Kelly
 
GLRB - Decent exposure
GLRB - Decent exposureGLRB - Decent exposure
GLRB - Decent exposureMatt Yoho
 
Desenvolvimento web com Ruby on Rails (parte 4)
Desenvolvimento web com Ruby on Rails (parte 4)Desenvolvimento web com Ruby on Rails (parte 4)
Desenvolvimento web com Ruby on Rails (parte 4)Joao Lucas Santana
 
Mason - A Template system for us Perl programmers
Mason - A Template system for us Perl programmersMason - A Template system for us Perl programmers
Mason - A Template system for us Perl programmersJerome Eteve
 
Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Pedro Cunha
 
DRYing Up Rails Views and Controllers
DRYing Up Rails Views and ControllersDRYing Up Rails Views and Controllers
DRYing Up Rails Views and ControllersJames Gray
 
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 2RORLAB
 
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 Engine Yard
 
The Django Book - Chapter 7 forms
The Django Book - Chapter 7 formsThe Django Book - Chapter 7 forms
The Django Book - Chapter 7 formsVincent Chien
 
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendallRubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendalltutorialsruby
 
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendallRubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendalltutorialsruby
 
Getting started with Rails (3), Season 2
Getting started with Rails (3), Season 2Getting started with Rails (3), Season 2
Getting started with Rails (3), Season 2RORLAB
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the FinishYehuda Katz
 
Search Form for Rails
Search Form for RailsSearch Form for Rails
Search Form for Railssinsoku listy
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overviewYehuda Katz
 
What's new in Rails 4
What's new in Rails 4What's new in Rails 4
What's new in Rails 4Fabio Akita
 
Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)lazyatom
 

Ähnlich wie Building a Rails Interface (20)

Getting started with Rails (4), Season 2
Getting started with Rails (4), Season 2Getting started with Rails (4), Season 2
Getting started with Rails (4), Season 2
 
Making Sense of Twig
Making Sense of TwigMaking Sense of Twig
Making Sense of Twig
 
GLRB - Decent exposure
GLRB - Decent exposureGLRB - Decent exposure
GLRB - Decent exposure
 
Desenvolvimento web com Ruby on Rails (parte 4)
Desenvolvimento web com Ruby on Rails (parte 4)Desenvolvimento web com Ruby on Rails (parte 4)
Desenvolvimento web com Ruby on Rails (parte 4)
 
Mason - A Template system for us Perl programmers
Mason - A Template system for us Perl programmersMason - A Template system for us Perl programmers
Mason - A Template system for us Perl programmers
 
Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11Ruby on Rails at PROMPT ISEL '11
Ruby on Rails at PROMPT ISEL '11
 
DRYing Up Rails Views and Controllers
DRYing Up Rails Views and ControllersDRYing Up Rails Views and Controllers
DRYing Up Rails Views and Controllers
 
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
 
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
 
The Django Book - Chapter 7 forms
The Django Book - Chapter 7 formsThe Django Book - Chapter 7 forms
The Django Book - Chapter 7 forms
 
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendallRubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendall
 
RubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendallRubyOnRails-Cheatsheet-BlaineKendall
RubyOnRails-Cheatsheet-BlaineKendall
 
Headliner
HeadlinerHeadliner
Headliner
 
Getting started with Rails (3), Season 2
Getting started with Rails (3), Season 2Getting started with Rails (3), Season 2
Getting started with Rails (3), Season 2
 
Rails 3: Dashing to the Finish
Rails 3: Dashing to the FinishRails 3: Dashing to the Finish
Rails 3: Dashing to the Finish
 
Search Form for Rails
Search Form for RailsSearch Form for Rails
Search Form for Rails
 
Rails 3 overview
Rails 3 overviewRails 3 overview
Rails 3 overview
 
What's new in Rails 4
What's new in Rails 4What's new in Rails 4
What's new in Rails 4
 
Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)Engines: Team Development on Rails (2005)
Engines: Team Development on Rails (2005)
 
Flask – Python
Flask – PythonFlask – Python
Flask – Python
 

Mehr von James Gray

In the Back of Your Mind
In the Back of Your MindIn the Back of Your Mind
In the Back of Your MindJames Gray
 
Amazon's Simple Storage Service (S3)
Amazon's Simple Storage Service (S3)Amazon's Simple Storage Service (S3)
Amazon's Simple Storage Service (S3)James Gray
 
Git and GitHub
Git and GitHubGit and GitHub
Git and GitHubJames Gray
 
Test Coverage in Rails
Test Coverage in RailsTest Coverage in Rails
Test Coverage in RailsJames Gray
 
Rails Routing And Rendering
Rails Routing And RenderingRails Routing And Rendering
Rails Routing And RenderingJames Gray
 
Sending Email with Rails
Sending Email with RailsSending Email with Rails
Sending Email with RailsJames Gray
 
Associations in Rails
Associations in RailsAssociations in Rails
Associations in RailsJames Gray
 
Rails Model Basics
Rails Model BasicsRails Model Basics
Rails Model BasicsJames Gray
 
Wed Development on Rails
Wed Development on RailsWed Development on Rails
Wed Development on RailsJames Gray
 

Mehr von James Gray (13)

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
 
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
 

Kürzlich hochgeladen

Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024Lonnie McRorey
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkPixlogix Infotech
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Alkin Tezuysal
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructureitnewsafrica
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfpanagenda
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Nikki Chapple
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observabilityitnewsafrica
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...itnewsafrica
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 

Kürzlich hochgeladen (20)

Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024TeamStation AI System Report LATAM IT Salaries 2024
TeamStation AI System Report LATAM IT Salaries 2024
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
React Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App FrameworkReact Native vs Ionic - The Best Mobile App Framework
React Native vs Ionic - The Best Mobile App Framework
 
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
Unleashing Real-time Insights with ClickHouse_ Navigating the Landscape in 20...
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical InfrastructureVarsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
Varsha Sewlal- Cyber Attacks on Critical Critical Infrastructure
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdfSo einfach geht modernes Roaming fuer Notes und Nomad.pdf
So einfach geht modernes Roaming fuer Notes und Nomad.pdf
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
Microsoft 365 Copilot: How to boost your productivity with AI – Part one: Ado...
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security ObservabilityGlenn Lazarus- Why Your Observability Strategy Needs Security Observability
Glenn Lazarus- Why Your Observability Strategy Needs Security Observability
 
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...Zeshan Sattar- Assessing the skill requirements and industry expectations for...
Zeshan Sattar- Assessing the skill requirements and industry expectations for...
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 

Building a Rails Interface

  • 1. Building the User Interface The C and V of MCV
  • 2. What is a Controller? A controller is the switchboard operator between your views, or web interface, and the model, or database Rails convention is “Fat models, skinny controllers.” 7 basic actions will accomplish 90% of desired behavior
  • 3. Generate a Controller generate/script controller controller_name Optionally, you can add action names and the controller will generate with those methods and view files Controllers are usually plural Generating the controller also creates the view directory
  • 4. Basic Controller Actions class PetsController < ApplicationController index() - list of like objects def index end new() - displays a new form def new end create() - saves new form def create input to DB end def edit edit() - displays an edit form end def update update() - saves edit form input to end DB def show end show() - displays single object def destroy end destroy() - deletes single object end
  • 5. Building the new() action def new @pet = Pet.new end
  • 6. Building the new() action new() asks the browser to display a def new @pet = Pet.new form end
  • 7. Building the new() action new() asks the browser to display a def new @pet = Pet.new form end Simply provide an empty object
  • 8. Building the new() action new() asks the browser to display a def new @pet = Pet.new form end Simply provide an empty object By rails convention, an action name and a view name should be the same
  • 9. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Pets Are People Too!</title> </head> <body> <% form_for @pet do |f| %> <%= f.error_messages %> <p><%= f.label :name %><br /><%= f.text_field :name %></p> <p><%= f.label :animal_type %><br /><%= f.text_field :animal_type %></p> <p><%= f.label :breed %><br /><%= f.text_field :breed %></p> <%= f.submit 'Create' %> <% end %> </body> </html> The new.html.erb form Rails uses ERB to gain access to Ruby code inside your HTML views
  • 10. Meet ERB Using ERB tags allows you to embed Ruby code directly in your HTML code The <% ... %> is used for block code (iterators and forms) or code you don’t want inserted The <%= ... %> inserts whatever is inside the tag The <%# ... %> comments out the tag contents
  • 11. A Closer Look <% form_for @pet do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> <%= f.text_field :name %> </p> <p> <%= f.label :animal_type %><br /> <%= f.text_field :animal_type %> </p> <p> <%= f.label :breed %><br /> <%= f.text_field :breed %> </p> <%= f.submit 'Create' %> <% end %>
  • 12. A Closer Look The first line sets up the code to follow <% form_for @pet do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> <%= f.text_field :name %> </p> <p> <%= f.label :animal_type %><br /> <%= f.text_field :animal_type %> </p> <p> <%= f.label :breed %><br /> <%= f.text_field :breed %> </p> <%= f.submit 'Create' %> <% end %>
  • 13. A Closer Look The first line sets up the code to follow <% form_for @pet do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> <%= f.text_field :name %> </p> <p> <%= f.label :animal_type %><br /> <%= f.text_field :animal_type %> </p> <p> <%= f.label :breed %><br /> <%= f.text_field :breed %> </p> <%= f.submit 'Create' %> <% end %>
  • 14. A Closer Look The first line sets up the code to follow <% form_for @pet do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> Inside the <p> tags are <%= f.text_field :name %> </p> displayed pieces of <p> <%= f.label :animal_type %><br /> code <%= f.text_field :animal_type %> </p> <p> <%= f.label :breed %><br /> <%= f.text_field :breed %> </p> <%= f.submit 'Create' %> <% end %>
  • 15. A Closer Look The first line sets up the code to follow <% form_for @pet do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> Inside the <p> tags are <%= f.text_field :name %> </p> displayed pieces of <p> <%= f.label :animal_type %><br /> code <%= f.text_field :animal_type %> </p> <p> <%= f.label :breed %><br /> <%= f.text_field :breed %> </p> <%= f.submit 'Create' %> <% end %>
  • 16. A Closer Look The first line sets up the code to follow <% form_for @pet do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> Inside the <p> tags are <%= f.text_field :name %> </p> displayed pieces of <p> <%= f.label :animal_type %><br /> code <%= f.text_field :animal_type %> </p> <p> Rails provides lots of <%= f.label :breed %><br /> <%= f.text_field :breed %> helpers so you don’t </p> <%= f.submit 'Create' %> have to spend time <% end %> writing lots of HTML
  • 17. A Closer Look The first line sets up the code to follow <% form_for @pet do |f| %> <%= f.error_messages %> <p> <%= f.label :name %><br /> Inside the <p> tags are <%= f.text_field :name %> </p> displayed pieces of <p> <%= f.label :animal_type %><br /> code <%= f.text_field :animal_type %> </p> <p> Rails provides lots of <%= f.label :breed %><br /> <%= f.text_field :breed %> helpers so you don’t </p> <%= f.submit 'Create' %> have to spend time <% end %> writing lots of HTML
  • 18. Why did I get an error? You haven’t told Rails what the path to your form is yet.
  • 19. Why did I get an error? You haven’t told Rails what the path to your form is yet.
  • 20. Why did I get an error? You haven’t told Rails what the path to your form is yet.
  • 21. Why did I get an error? You haven’t told Rails what the path to your form is yet.
  • 22. Routing Set routes in the config/routes.rb file ActionController::Routing::Routes.draw do |map| Provide the resource map.resources :pets name and Rails will map.connect ':controller/:action/:id' create the 7 routes map.connect ':controller/:action/:id.:format' end Setting a route requires a server restart
  • 23. Displaying the form The standard path for a resource is the controller name followed by the action We’ve collected the info. Now what?
  • 24. create() the Pet Object Create your Pet object def create @pet = Pet.new(params[:pet]) Check to see if it saved if @pet.save flash[:notice] = "Your Pet has been saved." Tell the browser how to redirect_to new_pet_path respond else flash.now[:error] = "There was a problem saving your Pet" What are params[] and render :action => "new" end flash[]? end
  • 25. Parameters Parameters are the pieces of information passed back to the controller action from the HTML form fields Rails collects them in a Hash Using Rails conventions, the Object name is the Hash name for the params
  • 26.
  • 28. Processing PetsController#create (for 127.0.0.1 at 2010-03-07 19:24:57) [POST] Parameters: {"commit"=>"Create", "pet"=>{"name"=>"Snow Paw", "breed"=>"Snowshoe Siamese", "animal_type"=>"Cat"}} Redirected to http://localhost:3000/pets Completed in 19ms (DB: 0) | 302 Found [http://localhost/pets] What the browser sees The server request shows the parameters coming in from the form as a Hash
  • 29. <% {:notice => "green", :error => "red"}.each do |message, color| %> <% next if flash[message].blank? %> <div style="color: <%= color %>;"> <%= flash[message] %> </div> <% end %> flash[ ] Messages flash[ ] messages give feedback to the user Rails automatically remembers the flash between requests
  • 30. def create @pet = Pet.new(params[:pet]) if @pet.save flash[:notice] = "Your Pet has been saved." redirect_to new_pet_path else ... end end Successful Create @pet was successfully saved to the database so a flash message displays and we’re redirected
  • 31. def create @pet = Pet.new(params[:pet]) if @pet.save ... else flash.now[:error] = "There was a problem saving your Pet" render :action => "new" end end Failed Create @pet failed a validation so the page was re- rendered with the flash message and errors
  • 32. Render and Redirect redirect_to() - redirects the browser to the target passed to it render() - unless otherwise indicated with a redirect_to() or explicit render() call, the view file named after the current action is displayed If you pass in an action name to render(), it will render that content instead render :action => "new"
  • 33. Whew! We made it. Now let’s check out the remaining five actions.
  • 35. index() index() shows a list or collection of objects def index @pets = Pet.all It sets a variable that end contains the collection desired
  • 36. index() index() shows a list or collection of objects def index @pets = Pet.all It sets a variable that end contains the collection desired def index Generally you want to @pets = Pet.all(:limit => 20) end set some kind of criteria on the find() to limit large lists
  • 37. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Pets Are People Too!</title> </head> <body> <table> <tr> <td>Name</td> <td>Animal Type</td> <td>Breed</td> <td>Actions</td> </tr> <% @pets.each do |pet| %> <tr> <td><%= h pet.name %></td> <td><%= h pet.animal_type %></td> <td><%= h pet.breed %></td> <td><%= link_to "View", pet_path(pet) %></td> </tr> <% end %> </table> <%= link_to "Add a New Pet", new_pet_path %> </body> </html> HTML for an index view link_to() takes the name of the link and the path See how I’m iterating through the collection?
  • 38. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Pets Are People Too!</title> </head> <body> <table> <tr> <td>Name</td> <td>Animal Type</td> <td>Breed</td> <td>Actions</td> </tr> <% @pets.each do |pet| %> <tr> <td><%= h pet.name %></td> <td><%= h pet.animal_type %></td> <td><%= h pet.breed %></td> <td><%= link_to "View", pet_path(pet) %></td> </tr> <% end %> </table> <%= link_to "Add a New Pet", new_pet_path %> </body> </html> HTML for an index view link_to() takes the name of the link and the path See how I’m iterating through the collection?
  • 39. From the Browser index() is the default action, so the address bar only needs the controller name
  • 40. show() show() - displays a single object Should set the object to display def show @pet = Pet.find(params[:id]) end The unique ID for a specific Pet comes in as a parameter from the browser
  • 41. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Pets Are People Too!</title> </head> <body> <% {:notice => "green", :error => "red"}.each do |message, color| %> <% next if flash[message].blank? %> <div style="color: <%= color %>;"> <%= flash[message] %> </div> <% end %> <p>Name: <%= h @pet.name %></p> <p>Animal Type: <%= h @pet.animal_type %></p> <p>Breed: <%= h @pet.breed %></p> <p><%= link_to "View All", pets_path %> | <%= link_to "Edit", edit_pet_path(@pet) %> | <%= link_to "Delete", pet_path(@pet), :method => :delete %></p> </body> </html> HTML for a show view by passing :method to link_to(), you can specify an HTTP action of that verb
  • 42. From the Browser show() needs an ID, so the address bar needs the controller name and the object ID
  • 43. edit() and update() edit() and update() are def edit very similar to new() @pet = Pet.find(params[:id]) end and create() def update @pet = Pet.find(params[:id]) They require the object if @pet.update_attributes(params[:pet]) ID flash[:notice] = "Your Pet has been updated." redirect_to pets_path edit() renders the form else flash.now[:error] = "Something went while update() wrong." render :action => "edit" performs the actual end end update
  • 44. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Pets Are People Too!</title> </head> <body> <% form_for @pet do |f| %> <%= f.error_messages %> <p><%= f.label :name %><br /><%= f.text_field :name %></p> <p><%= f.label :animal_type %><br /><%= f.text_field :animal_type %></p> <p><%= f.label :breed %><br /><%= f.text_field :breed %></p> <%= f.submit 'Update' %> <% end %> </body> </html> HTML for the edit view When you select the edit() action, the form pulls in the object data
  • 45. From the Browser edit() needs an ID, so the address bar needs the controller name, the object ID and the action
  • 46. destroy() destroy() doesn’t have def destroy @pet = Pet.find(params[:id]) a view @pet.destroy flash[:notice] = "The Pet was deleted." redirect_to pets_path Passing a :method end parameter into link_to() forces the use of the <p> passed HTTP verb <%= link_to "View All", pets_path %> | <%= link_to "Edit", edit_pet_path(@pet)%> | Remember the show <%= link_to "Delete", pet_path(@pet), :method => :delete %> </p> HTML code?
  • 47. From the Browser Clicking Delete redirects the user back to the index view. Notice the missing fish?
  • 49. Building an Interface Lab Your book has instructions on how to create controllers to show your models

Hinweis der Redaktion