E N D
1. Ruby on Rails
2. 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
Dont Repeat Yourself (DRY)
Every piece of knowledge should be expressed in just one place
3. MVC
4. 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.
5. Rails and MVC
6. 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
7. 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
8. 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
9. Creating a Ruby-on-Rails Project create a projects directory
rails my_app
10. 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
11. 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
13. 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
14. 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 -%>
15. Rails Dynamic Content We may also embed controller instance variables
16. Rails linking Add another action and view - goodbye
17. 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 %>
18. 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
19. Sessions Ruby manages sessions for you.
session hash
session[loggedin] = true
session[loginrole] = admin
To reset the session use
reset_session
20. 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 -%>
21. 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
22. 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
23. 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
24. Layouts
25. Default Page By default, whatever doesnt 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