RubyOnRails

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
class SayController < ApplicationController
end
– pretty minimal
• Add an action and a corresponding view
HelloWorld/
app/
controllers/
say_controller.rb
models/
views/
say/
hello.html.erb
http://localhost:3000/say/hello
class SayController < ApplicationController
def hello
end
end
<html>
<head><title>Hello World</title></head>
<body>
<h1>Hello World</h1>
</body>
</html>
Rails naming conventions
• Controller Naming
–
–
–
–
–
URL
File
Class
Method
Layout
http://……/say/hello
app/controllers/say_controller.rb
SayController
hello
app/views/layouts/say.html.erb
–
–
–
–
URL
File
Helper
File
http://……/say/hello
app/views/say/hello.html.erb
module SayHelper
app/helpers/say_helper.rb
• View Naming
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
<html>
end
<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 + "\n"
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
app/views/layouts/application.html.erb
<html>
<head><title>Hello & Goodbye World</title></head>
<body>
<%= yield %>
</body>
</html>
app/views/say/goodbye.html.erb
<h1>Goodbye World</h1>
<%= link_to "Hello", :action => "hello" %>
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
# You can have the root of your site routed by hooking up ''
# -- just remember to delete public/index.html.
# map.connect '', :controller => "welcome"
map.connect '', :controller => 'say', :action => 'hello'
– remember to remove public/index.html