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.
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.
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.
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.
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.
Now start InstantRails.exe
It will probally ask to update the configuration files. Select OK.
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.
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.
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
Open a ruby window by using InstantRails-->I icon on top left-->Rails Applications-->Open Ruby Consolw Window
Executing the First Commands
In your newly opened console window type : rails comment
Create the Controller
Now we make the controller.
Navigate to the new directory: cd comment
Next type: ruby script/generate controller Comments
Navigate to the new directory: cd comment
Next type: ruby script/generate controller Comments
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
This is nearly identical to the last one.
Type: ruby script/generate model Post
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.
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.
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.
Under the line that starts create_table insert
t.column :name, :string
t.column :comment, :text
Save file and close the SciTE.
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
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
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)
You may have to unblock this at your firewall.
Next go to -->I icon-->Configure-->Database(via PhpMyAdmin)
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.
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.
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
And we will start the server.
First Navigate to the comment directory by cd comment
Now input: ruby script/server
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
Go to -->app\controllers and open the comments_controller.rb file in SciTE
Edit the Controller
In SciTE edit the controller by adding
scaffold :post
right in the middle.
scaffold :post
right in the middle.
Migrate the Db
In your first console window type
rake db:migrate
This will prep the files and load them for viewing.
rake db:migrate
This will prep the files and load them for viewing.
Look at What We Have Done
Open your web browser to http://localhost:3000/comments and see how it looks.