Title: Ruby On Rails
1Ruby On Rails
2The MVC Diagram
3The MVC Model
- The business logic just mentioned is a part of
the Model - Along with this is the data that it interacts with
4The MVC View
- The view is geared at the User Interface
- This can be the text on the page
- Your input devices such as text boxes etc
5The MVC Controller
- This interfaces between the model and the view
- It lets the model know what the user is doing in
the view - This can be user input from the mouse or the
keyboard
6MVC for web applications
- Model - The data in the database and the
algorithms used to manipulate it - View - The front end HTML pages
- Controller - Gathers the data based on user input
thats used as content for the HTML
7The simplest RoR application
- With just a few commands RoR will give you a
functioning DB backended web application - All of the necessary ruby code, html and database
connectivity will be created
8Rails First Steps
- Command windows/Terminals will be used a lot
- With all the software installed we can run.
rails MyFirstApp -d mysql - This creates our basic structure, but its pretty
empty so far! It also specifies mysql as the
database - This applies the naming logic used by Rails
9Screenshot
10Trying out!
- To be able to view the web pages we are creating
we need to run the server - This is found in the scripts subdirectory
- The server will act like a normal web server such
as Apache etc - This allows us to view our ruby generated HTML in
a browser - Run. script/server
11The server start up
12Server Problems??
- Sometimes Mongrel (the server) doesnt kill its
processes - You may get an error message saying address still
in use
13Address in use?
14Kill those processes!
- To kill those processes properly try something
like - kill -HUP lsof -t -I TCP3000
15Our first page
- We can now view the RoR app with a browser
- We can do this locally as we have the Mongrel web
server running - http//127.0.0.13000/
- This is the local address we use
16Our first page output
17The welcome page
- We have so far only created the bare bones of the
application - There is no database or pages yet
- We can create these in various ways
18Lets make it simple
- As RoR is database orientated we need one even if
we arent going to use it - Database work uses the rake utility
- To create an empty database for the project.
rake dbcreate
19Databases
- The database details are held in
config/database.yml - There are 3 databases
- Development
- Test
- Production
- You can edit these as you wish
- They look like this..
20Database Details
development adapter mysql encoding utf8
database MyFirstApp_development username
root password socket /tmp/mysql.sock
21Getting Rails to Ignore databases
- Open config/environment.rb
- And change to
config.frameworks - active_record,
active_resource, action_mailer
config.frameworks - active_record
- This will make it exclude ActiveRecord
22Using Controllers
- As we have seen before controllers are the link
between the View and the Model - We can use rails to create one for us
- script/generate controller MyTest
- Now we have our controller in app/controllers/my_t
est_controller.rb
23First Look at Ruby
- If we look at this file it is pretty empty
- We can define controller methods in this file.
- These are accessed when we enter a specific URL
- So lets create an index page for our site
24An Index Page
- In our controller page app/controllers/my_test_con
troller.rb we can add defs for each page that we
browse to - So for our http//127.0.0.13000/My_Test/index
URL we can create an index def in our MyTest
controller - This will be called when the URL above is entered
25The code and output
class MyTestController lt ApplicationController
def index rendertextgt
"This is my index page" end end
26Using Views
- Lets now get a controller and a view to
communicate - Create a new controller with.
script/generate controller MySecondTest index
Notice the index argument. This creates an
empty action (method) index in the controller,
as we did manually previously
27Outputting to the browser
- If we leave the index action in the controller
empty we will get
28Controller and View data sharing
- We can add a variable to our controller in the
VariableTest action - This will be accessible from our View
- We use the _at_ to show its a variable that can be
accessed by the View
29Adding a variable code
30View missing
- If we try and access this action we it says
Template Missing (View) - So we need to sort that out!
31Controller and View
- This message comes as we havent specified
anything to render from the controller nor the
view. - We have seen how to pass on instructions from the
controller with the rendertext command - But you can use the View to give output
- This output is normally related to the data that
the Controller has grabbed from the Model
(database)
32Creating the View
- If we add a view for this VariableTest action in
app/views/my_second_test/VariableTest.html.erb - To display the contents of the variable in a
browser we just need some ruby code - lt _at_MyVariable gt
- We can add normal HTML to this file as well if we
like
33Viewing the View!
34Using the Model
- So we have seen how Views and Controllers pass
variables and how they are called from URLs - Now we can look at the backend where the
databases and Business Logic reside
35Database Creation Using MySQL
- Do this manually to get an understanding of how
it operates - Use some GUI tool to create and manipulate the
Databases - Create some new DB in MySQL called MyDatabase
- Rails needs to know which database it is
connecting to so change this in
config/database.yml
36database.yml
development adapter mysql encoding utf8
database MyDatabase username
root password socket /tmp/mysql.sock
37Database Bits and Bobs
- Add a table, and some fields
38Create the Model class
- We have previously created Views and Controllers
- Similarly we must create a Model class for the
database - This is simple in RoR
- script/generate model Bike
39A note on Naming Conventions
- Here we have created a database table called
bikes - We have then created a model class definition
Bike - RoR is clever with its naming conventions
- It knows that Bike maps to the database table
bikes
40The Controller for our Bike app
- script/generate controller Bike
- Remember Scaffolding?
- This creates all the basics of our app code
- So in app/controllers/bike_controller.rb we can
set a command to do all of the donkey work with
the database for add, delete etc
41Controller code for bike
42New code to access the database
- You can now add etc bikes to the database
- http//127.0.0.13000/bike/new
- But didnt we say that RoR is a rapid devlopment
platform? - This route is very long winded, and also not used
much anymore - So we can do all of that with a simple couple of
RoR commands!
43The Cars database
- rails MyCarApp
- script/generate scaffold car modelstring
manufacturerstring colourstring yeardecimal - rake dbmigrate
- Go to the URL http//localhost3000/cars
44How does it look?
45Looks
- Ok so it doesnt look pretty but in 3 commands we
have a fully functional web app - We have the full structure and ruby code
- Thats not bad going!
46Generated Code
- Scaffold has generated the bare bones code
- If we look in app/views/cars we can see the basic
pages for show, new edit and index. We can hack
away at these as much as we want to make it
prettier! - Just add normal HTML mixed with Ruby
47Adding HTML
48The Code