Screencast: Obligatory Rails Demo
4 Comments »
Agenda
- Application: LinkWizz, similar to http://del.icio.us
- Make an app with simple tools (MySQL command line, text editor).
- Not showing scaffolding at this point.
- Will not create database the Rails way (using migrations), but with ordinary SQL statements.
- Will be showing layouts. It’s a little complicated at this point, but important.
- Will add additional screencasts later to show other features of Rails.
- Sequence:
- rails command to create project;
- start up server;
- look at directory structure;
- browse to app (http://localhost:3000)
- generate Link controller, and define list method and list.rhtml view
- Create database and “links” table
- Edit config/database.yml
- Generate Link model (notice: database table is plural “links” but model name is singular “Link”)
- Add code to list.rhtml view to display links
- Add to Link controller add method and add.rhtml view
- Add code to add.rhtml for the form
- Add a save method to the Link controller
- Setup layout and stylesheets
- Add code to display the flash
View (you will want to maximize your browser; to go “full-screen” in Firefox, press F11; then to turn off full-screen, press F11 again)
And the code is here.
September 26th, 2007 at 12:44 pm
This is just a minor note, but I noticed you were using content_for_layout in your demo instead of yield. I’ve often wondered which one was better to use or preferred by the Rails community? I read the following, although I have read also that content_for_layout is “faster”…
“Even the old @content_for_layout in the layout is deprecated in favor of just using yield in its place. Also content_for(’some_fragment’) is now accessed with yield :some_fragment rather than @content_for_some_fragment.”
http://weblog.rubyonrails.com/2006/04/25/use-params-not-params/
September 26th, 2007 at 2:00 pm
The best practice is to use yield.
Had I used yield, I would have confused everyone because I would have had to stop and explain it. Whereas we were also using < %= %> to copy stuff into the response elsewhere.
Tonight there will be a 5-slide digression on “yield.”
September 28th, 2007 at 5:21 pm
1. That is pretty cool.
2. Can you create controllers that have no association with a view?
3. Do all “models” have to be backed up by a persistent source? What if I want to create the Link model without wanting to store it, but yet want to use it somehow, can it be done? I assume that I would have to create it in the “app/helpers” area, correct?
4. Maybe a “Myspace” clone for the next screen cast???
September 28th, 2007 at 6:16 pm
Trung: Strictly speaking, controllers aren’t associated with a view. By convention, the page will be rendered with a template of the same name as the method in the controller. But you might have a controller where all of the methods redirect to various other pages.
For model: It depends what you mean by “model.” Our model classes are based on ActiveRecord, which presumes that the model is “backed” by a table.
MySpace: The next Rails screencast needs to talk about the relationships between models — it will probably be a good idea to keep extending LinkWizz, but maybe we can build into it some social networking features.
Perhaps you can clone MySpace for your final project.