Introduction: Getting Started With Rails
Have you ever seen an website that was so cool and interactive you immeditaly wonderded "How did they do that?".
Well I am going to show you one way that it can be done. Of course we won't be creating a site as great as Instructables to start with, but the principles can easily grow into any type of site you can imagine with a bit more work.
Teachers! Did you use this instructable in your classroom?
Add a Teacher Note to share how you incorporated it into your lesson.
Step 1: Don't Repeat Yourself
"Don't repeat yourself", this statement is typically followed by a "What?"...then you repeat "Don't repeat yourself".
One of the main principles of Ruby is D.R.Y. so we are going to stick by this as much as possible.
Lets start by finding a fully functional framework.
Instant Rails is a great project. Click on the link and unzip the file to a directory that has no spaces (like C:\). Don't worry for a small file it takes a long time to unzip, this is because of the many small folders that it contains.
Step 2: Building a Comment Page
First make sure you are not running any other web server such as IIS. If you are shut that down now.
Now start InstantRails.exe
It will probally ask to update the configuration files. Select OK.
Step 3: A Little Background
A bit of a background on how Ruby on Rails actually works.
Ruby on Rails works using a MVC structure. That is Model, View, and Controller.
The Model is the part that does all the work, it will be the number cruncher, the storage structure etc....
The View is just that a viewer. This is going to generally be the format that is presented to a web page in our demonstartion.
The Controller is the part that takes the user input and passes it on to the Model.
So you see the User enters text into the Controller, that is then passed to the Model where it does something and spits it back out to the View.
Simple enough, but keep it in mind as we go through the next few steps.
Step 4: Create the Application
First lets create a rails app
Open a ruby window by using InstantRails-->I icon on top left-->Rails Applications-->Open Ruby Consolw Window
Step 5: Executing the First Commands
In your newly opened console window type : rails comment
Step 6: Create the Controller
Now we make the controller.
Navigate to the new directory: cd comment
Next type: ruby script/generate controller Comments
Step 7: Create the Post Model
Now we generate a new model called Post.
This is nearly identical to the last one.
Type: ruby script/generate model Post
Step 8: Finnaly Some Real Ruby Stuff
Go in your explorer window to -->rails_apps-->comment-->db-->migrate
Open the file 001_create_posts.rb
It should open with SciTE, if it does not you may want to use that for the rest of this demo.
Step 9: Add the Fields
Here we will add the Name and Comment fields.
Under the line that starts create_table insert
t.column :name, :string
t.column :comment, :text
Save file and close the SciTE.
Step 10: Where Is the Config
Quick steps to find what your database should be called.
Go to the config folder and open the database.yml file with SciTE.
In the first section after the comments (the green text) you will see the development section, that is the database we will be using for the time being.
The name of our database should be comment_development
Step 11: Create the Database
Now in the InstantRails command window start apache.
You may have to unblock this at your firewall.
Next go to -->I icon-->Configure-->Database(via PhpMyAdmin)
Step 12: Creating the Database
In the webpage find the box titled Create New Database and fill in the name of the database we need.
Here it is : comment_development
Accpet the defaults and close the window.
At this time go ahead and shut down apache by the same method we started it but use the stop button this time instead.
Step 13: Start the Ruby Server
Open a new console window (you should now have 2 open).
And we will start the server.
First Navigate to the comment directory by cd comment
Now input: ruby script/server
Step 14: Set Up the Controller
Now we want to set up the controller for the server.
Go to -->app\controllers and open the comments_controller.rb file in SciTE
Step 15: Edit the Controller
In SciTE edit the controller by adding
right in the middle.
Step 16: Migrate the Db
In your first console window type
This will prep the files and load them for viewing.
Step 17: Look at What We Have Done
Open your web browser to http://localhost:3000/comments and see how it looks.