Difference between revisions of "Emergent Web Technologies Spring 2009 Class 6"
m |
|||
Line 61: | Line 61: | ||
ruby script/generate controller Widgets | ruby script/generate controller Widgets | ||
− | Notice that a controller has been created at app/controllers/widgets_controller.rb | + | Notice that a controller has been created at |
+ | |||
+ | app/controllers/widgets_controller.rb. | ||
+ | |||
+ | Open the file and add an new action to it. Let's call it list: | ||
+ | |||
+ | def list | ||
+ | render :text => "This is a list" | ||
+ | end | ||
+ | |||
+ | Start the server back up again and open http://localhost:3000/widgets/list. You should see the text "This is a list". | ||
+ | |||
+ | Take out the render text line and create a template instead. Open the folder at | ||
+ | |||
+ | /app/views/widgets/ | ||
+ | |||
+ | and create a new file called | ||
+ | |||
+ | list.html.erb | ||
+ | |||
+ | Let's make the page a little more dynamic by passing a variable to the view. Create a new variable to pass to the view for the number of widgets | ||
+ | |||
+ | def list | ||
+ | @widget_count = 50 | ||
+ | end | ||
+ | |||
+ | Update your view so that it shows the number of widgets | ||
+ | |||
+ | There are <%= @widget_count %> widgets |
Revision as of 22:18, 4 March 2009
Contents
Introduction
This week we'll finish up with an introduction to Ruby and move on to Ruby on Rails. We'll start with an introduction to web frameworks in general, and then move on to Model View Controller. Finally, we'll build a small Rails app in class to demonstrate how much more quickly you can build applications if you have a framework to start with.
In Class Activity 1
Develop a simple Ruby class that represents a Vehicle. The Vehicle class should have public properties for a registration id and a color. It should also have functions for starting and stopping the vehicle. Make the functions write "started" and "stopped" to the console when the car is started or stopped.
Develop a Car class that inherits from the vehicle class. The Car class should have a new public property for the number of doors. You should should be able to pass in the color and number of doors to the constructor for the car.
Once you have both of the classes created, test out your code by creating a new car and make sure that you can start it and stop it. Also, make sure that you can set the registration id and read back the color and number of doors by printing them out to the console.
Your test code should look something like this
car = Car.new(4, :green) car.registration_id = 5000 puts car.registration_id puts car.color puts car.number_of_doors
Remember that a Ruby class looks like this
class Vehicle end
Commands to Run Before Developing With Rails
Before you start developing with Ruby on Rails, you'll want to make sure that all of the code associated with Rails is current. These commands will make sure that everything is up to date with the latest versions.
Open the Instant Rails application and right click on the icon in the taskbar. Choose Rails Applications > Open Ruby Console Window. Run this command first
gem install rubygems-update
Once the first command has completed, run this command
update_rubygems
Finally, run this command
gem update rails
You should now be up to date.
Let's Create Rails Application
Create your Rails application
rails my_test_app
You should see a list of files that have been created for your application. Let's see if it works.
cd my_test_app ruby script/server
Now open a browser and navigate to http://localhost:3000.
Now, let's start creating some pages. Create a new controller for a page about widgets. First you'll need to stop the server by hitting ctrl + c in your console window. Then type this:
ruby script/generate controller Widgets
Notice that a controller has been created at
app/controllers/widgets_controller.rb.
Open the file and add an new action to it. Let's call it list:
def list render :text => "This is a list" end
Start the server back up again and open http://localhost:3000/widgets/list. You should see the text "This is a list".
Take out the render text line and create a template instead. Open the folder at
/app/views/widgets/
and create a new file called
list.html.erb
Let's make the page a little more dynamic by passing a variable to the view. Create a new variable to pass to the view for the number of widgets
def list @widget_count = 50 end
Update your view so that it shows the number of widgets
There are <%= @widget_count %> widgets