Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Ruby on Rails

on

  • 643 views

 

Statistics

Views

Total Views
643
Views on SlideShare
643
Embed Views
0

Actions

Likes
0
Downloads
2
Comments
0

0 Embeds 0

No embeds

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Ruby on Rails Ruby on Rails Presentation Transcript

  • Ruby on Rails
  • Ruby on Rails
    • Web Framework for Ruby
    • Designed to make it easier to develop, deploy, and maintain web applications
    • Design with Model-View-Controller
      • almost forced
    • Also based on DRY concept
      • Don’t Repeat Yourself (DRY)
      • Every piece of knowledge should be expressed in just one place
  • MVC
  • Rails and MVC
    • Incoming requests are first sent to a router.
    • Router determines
      • where in the application the request should be sent and
      • how the request itself should be parsed.
    • Router identifies a particular method in the controller code.
      • called an action
    • The action may
      • look at data in the request,
      • interact with the model,
      • cause other actions to be invoked.
    • The action prepares information for the view, which renders something to the user.
  • Rails and MVC
  • Rails and MVC
    • 2 Main components in Rails
      • Action Pack and Active Record
    • Active Record
      • Model support
      • Simplifies database interaction
      • Addressed in another lecture
    • Action Pack
      • Controller and View are tightly coupled
      • Single Rails component
      • View code and Controller code are separate
  • View Support
    • View is responsible for creating all or part of a page
    • Can include dynamic content
    • .html.erb files
      • may contain embedded code much like php, jsp
      • developers must be careful to maintain clean separation of Model, View, and Controll code
    • Rails also supports the creation of page templates and helpers
      • helpers are methods that make it easier to write page templates
  • The Controller
    • Logical center of your application
    • The controller is a class that is initially generated by rails
      • methods map to actions and are logically connected to views
    • Controller class variables are available to views
      • Controller sets the variable
      • Views then use the variable
    • Also responsible for routing, caching, and session management
  • Creating a Ruby-on-Rails Project
    • create a projects directory
    • rails my_app
  • More Rails
    • Rails has scripts to help you
      • run the scripts
      • customize files to fit your needs
    • Rails goal
      • Always have something working
      • Incremental development
      • Agile
  • Hello World
    • rails HelloWorld
    • ruby script/generate controller Say
    • Look at controller
      • app/controllers/say_controller.rb
      • pretty minimal
    • Add an action and a corresponding view
      • class SayController < ApplicationController
      • end
  • HelloWorld/ app/ controllers/ say_controller.rb models/ views/ say/ hello.html.erb
      • class SayController < ApplicationController
      • def hello
      • end
      • end
      • <html>
      • <head><title>Hello World</title></head>
      • <body>
      • <h1>Hello World</h1>
      • </body>
      • </html>
    http://localhost:3000/say/hello
  • Rails naming conventions
    • Controller Naming
      • URL http://……/say/hello
      • File app/controllers/say_controller.rb
      • Class SayController
      • Method hello
      • Layout app/views/layouts/say.html.erb
    • View Naming
      • URL http://……/say/hello
      • File app/views/say/hello.html.erb
      • Helper module SayHelper
      • File app/helpers/say_helper.rb
  • Rails Dynamic Content
    • We can embed dynamic content in .html.erb files
      • embedded ruby code
    • We may embed
      • expressions to be evaluated
        • <%= Time.now %>
        • <%= 1.hour.from_now %>
      • ruby code
        • <% 3.times do |count| -%>
        • <%= count %>: Hello<br>
        • <% end -%>
    putting the - sign(-%>) at the end eliminates newlines for the ruby code lines in the generated html
  • Rails Dynamic Content
    • We may also embed controller instance variables
      • class SayController < ApplicationController
      • def hello
      • @time = Time.now
      • end
      • end
      • <html>
      • <head><title>Hello World</title></head>
      • <body>
      • <h1>Hello World</h1>
      • It is now <%= @time %>
      • </body>
      • </html>
  • Rails linking
    • Add another action and view - goodbye
      • class SayController < ApplicationController
      • def hello
      • @time = Time.now
      • end
      • def goodbye
      • end
      • end
      • <html>
      • <head><title>Goodbye World</title></head>
      • <body>
      • <h1>Goodbye World</h1>
      • </body>
      • </html>
  • Rails Page Linking
    • Could simply add anchor
      • <a href=“/say/goodbye”>Goodbye</a>
    • Fragile
      • Could move
      • Too much reliance on rails url format
    • Use link_to
      • <%= link_to “Goodbye”, :action => “goodbye” %>
  • Form Data
    • Form data is available in the params hash
      • Holds all the data passed in a browser request
    • name = params[“name”]
    • age = params[“age”]
    • age = params[:age]
      • remember :age maps to “age”
    • Available whether using GET or POST
  • Sessions
    • Ruby manages sessions for you.
    • session hash
      • session[“loggedin”] = true
      • session[“loginrole”] = “admin”
    • To reset the session use
      • reset_session
  • Having some fun
    • List files in a directory
      • Add index action to say_controller.rb
        • def index
        • @files = Dir.glob(‘*’)
        • end
      • Add the index view file – index.html.erb
        • <% for file in @files -%>
        • file: <%= file %>
        • <% end -%>
  • Helpers
    • A module containing methods to help a view
      • each controller gets its own helper
    • Use to reduce the amount of code needed by view
    module SayHelper def file_list list = '<ol>' for file in Dir.glob('*') do list += '<li>' + file + &quot; &quot; end list += '</ol>' end end <html> <head><title>Index</title></head> <body> <h1>File listing</h1> <%= file_list %> </body> </html>
  • Other Helpers
    • Rails comes with a bunch of helpers
      • <%= distance_of_time_in_words(Time.now, Time.local(2007, 12, 25) %>
        • 62 days
      • <%= number_to_currency(123.45) %>
        • $123.45
      • <%= number_to_phone(2125551212) %>
        • 212-555-1212
      • <%= number_with_delimiter(12345678, “,”) %>
        • 12,345,678
    • Many others - look at the Action View RDoc
  • Layouts
    • Templates for views
      • app/views/layouts
    • One per view or application
      • viewname.html.erb
      • application.html.erb
    • Specify look and feel.
    • Eliminate those portions from corresponding views.
      • only need the partial .html.erb code in view
  • Layouts <html> <head><title>Hello & Goodbye World</title></head> <body> <%= yield %> </body> </html> <h1>Goodbye World</h1> <%= link_to &quot;Hello&quot;, :action => &quot;hello&quot; %> app/views/layouts/application.html.erb app/views/say/goodbye.html.erb
  • Default Page
    • By default, whatever doesn’t match to a controller and action goes to the public directory
    • Can change default page to a controller and action
      • edit config/routes.rb
      • remember to remove public/index.html
    # You can have the root of your site routed by hooking up '' # -- just remember to delete public/index.html. # map.connect '', :controller => &quot;welcome&quot; map.connect '', :controller => 'say', :action => 'hello'