Presentation is loading. Please wait.

Presentation is loading. Please wait.

Model – View – Controller Pattern

Similar presentations


Presentation on theme: "Model – View – Controller Pattern"— Presentation transcript:

1 Model – View – Controller Pattern
Carol Wolf CS 396X

2 Models ## A class to work with the database
class Book < ActiveRecord::Base def self.find_books find(:all, :order => "author") end def self.find_a_book(title) @book = find_by_title(title)

3 Controllers Classes with methods that receive requests from the client, query the database (through the model) and then respond back to the client. These are subclasses of the ApplicationController class. Controllers are the most complicated classes in Rails and do the most work.

4 class LibrarianController < ApplicationController
def index end def list_books @books = Book.find_books def find_book @params = params[:book] @book = respond_to do |format| != nil format.html else flash[:notice] = 'Book was not found.' format.html { render :action => "index" }

5 Views – Request Page Views are the web pages sent to the client.
They are written in a combination of html and ERB, embedded Ruby. Views come in pairs, the request page that the client sends to the server and the response page that the server sends back. <h1>Librarian</h1> <h2>List all books</h2> <h3><% form_for :book, :url => {:action => :list_books} do |form| %> <p><%= submit_tag "List Books" %></p> </h3> <% end %>

6 Views – Response Page <h1>List of Books</h1> <table> <tr> <th>ISBN</th> <th>Author</th> <th>Title</th> </tr> <% for book %> <td><%=h book.isbn %></td> <td><%=h book.author %></td> <td><%=h book.title %></td> <% end %> </table> <br /> <%= link_to 'Back', '/librarian' %>

7 Request and Response Pages


Download ppt "Model – View – Controller Pattern"

Similar presentations


Ads by Google