First step to rails… Installing it.
Okay, so, it’s much easier if you have RubyGems installed, and if you do:
[sudo] gem install -y rails
You can then start a project.
rails [--database=sqlite3] [options] [projectname]
You could read the README, but that’s complicated and this is a lot easier.
First of all, “cd” into your new project.
cd [projectname]
Models
Then, you can start with a Model.
In Rails, Models are like collections of objects. Behind them is a technology called ActiveRecord. Using this, we can…
- create users… (script/generate model user name:string password:string about:string)
And other thing-a-ma-bobs.
IMPORTANT! Make sure you edit the db/migrate/[number]_create_[modelname].rb file first so that where it says create_table(…) there is a line like this right under it.
t.column :id, :integer, :null => false
Strangely, the generator doesn’t automatically put this in… I’ll have to file a bug report…
Rendering the information (a.k.a. Controllers)
Well, there is something called scaffolding which you could put in your app/controllers/[controllername]_controller.rb file… But I’m gonna teach you the hard , kind-of easy way.
First, the same way we generate a model:
script/generate controller [controllername]
# ...or...
script/generate scaffold [controllerandmodelname]
The app/controllers/[controllername]_controller.rb file should look like this
class [CamelCaseControllerName] < ApplicationController
layout "[layoutname]"
def [viewmethod]
[helping-hands]
end
#...
end
Views
I actually prefer using Haml rather than the usual RHTML, so go ahead and use RubyGems:
[sudo] gem install -y haml
#and
cd ..
haml --rails [projectname]
cd [projectname]
Great, your project is now Haml-enabled. What do I do now?
Fire up app/views/layouts/[layoutname].haml, and put in
!!!
%html
%head
%title= @title
%body
= yield
And, so, a beginning.
You may now delete everything in the public/ folder except the folders images/, stylesheets/, and javascripts/. After that:
script/server
Then fire up Firefox and go to:
http://localhost:3000/[controllername]/
Something should show if you configured everything correctly.