loading
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.

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

scaffold :post

right in the middle.

Step 16: Migrate the Db

In your first console window type

rake db:migrate

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.
I tried it out : twice. I can't get it to work! Now, i have no idea what i'm doing at all, and i'm just following steps, so maybe there's a small syntax error or something i just don't know how to see that i'm doing wrong. When i try to launch the page, at the end, i get Routing Error: no route found to match &quot;/comment&quot; with {:method=&gt;:get} .<br/><br/>Don't mean to make you troubleshoot this, though, i just couldn't get it running. Your tutorial was still fun to follow, i would never have attempted something like this before.<br/>
That is my fault, you should go to <br/><br/><a rel="nofollow" href="http://localhost:3000/comments">http://localhost:3000/comments</a><br/><br/>in your browser. <br/>Since we named the controller Comments (plural) that is where we need to put it in the address.<br/>I will fix it in the instructable.<br/>
that fixed it! Wow, this is a pretty impressive comment tool for such a short amount of typing. Can you point me to some resources where I could learn to modify this or create my own? This is pretty powerful stuff, clearly. Thanks for the introduction!
This is some really cool stuff here.<br/><br/><a rel="nofollow" href="http://www.rubyonrails.org/screencasts">http://www.rubyonrails.org/screencasts</a><br/><br/>
Dude (or dudette?), I LOVE you. Thank you for posting this. I'm speechless. I cannot express how useful this is to me. Instead of me having to scour the web and figure out this crap you've laid it down on my desk. Thanks to you!!! And please, keep it up. I didn't know what this was about at first, though, so I think a better intro would help with that. Again, THANKS!
This is great. I've been meaning to learn a web programming language, as I've been feeling rusty, and this is a perfect starting point.
Cool :) I wish I had time during school to work with web code/apps more :P
This is actually for a class, I just like to kill 2 birds with one stone!
wow looks really simple but really complex at the same time you have a good knowledge of programming i think

About This Instructable

6,602views

18favorites

License:

More by pr0xie:Getting Started With Rails 
Add instructable to: