Wednesday, January 12, 2011

Ruby on rails Installation

1 Goto RubyForge and get latest version download of the Ruby one-click Installer for Windows.
2 Install it by running the setup program. I recommend installing it to c:\ruby
3 Download and install RubyGems.
4 When Ruby and RubyGems have both been set up, run this in a terminal/command prompt window to install Ruby on Rails and all its dependencies:
Command : gem install rails --include-dependencies

5) Create a folder to hold your rails Apps. I use c:\rails.

6) Create a test application by issuing this command in a terminal/command prompt:
Command : rails testApp

7) You should then see a folder called testApp in c:\rails. Go into the testApp folder in a terminal/command prompt and type this.
Command : ruby script/server

8) Open your browser and go to http://localhost:3000/.

Installation of Mysql

1. Install MySQL Admin and run it.
2. Create a new database connection by clicking the icon shown (or press Ctrl-N):


3. Fill out the information as shown in the picture below:


Name: This is any name you want – for GUI purposes only. Call it “My Development Database”.
Host name: This would be either “localhost” or “127.0.0.1″. On some weird configs “localhost” might not work, so try to use either one.
User name: “root”. Leave it as is.
Password: your root password. Remember setting it when installing MySQL?
Make this server the default connection: If you do not have any other databases, tick this box. By running MySQL CC next time, you will automatically connect to the database.
Now, click the “Test” icon, and if successful, click “Add”.
4. Create the database.
Go to the main console. It now should have your database “My Development Database”. Check that it is connected, if not click the “Connect” icon.
Right click on the “Databases” folder and select “New database”. You will be presented with the following prompt. Just name the database “firstproject”, although it does not really matter how you call the database.


5. Now, back to the main console window, you should see the database name “firstproject” in the list of databases for your server. Double click on the database and then click on the “Tables” subfolder. It should look something like this:


6. Now, the database for your project has been created, you need to create a test table. Right click the “Tables” folder and choose “New table”. Let’s make two fields: id (type: int) and title (type: varchar).
“id” field is required by Rails and is the usual feature for the majority of tables. You do not usually insert the “id” directly, so we need to make sure that “id” is never empty (not null) and is automatically incremented (0,1,2,3,…). Also, make sure that the “id” is written in lowercase, for Rails to understand that this is your unique id field.


Now, let’s define “id” as our primary field. Go into the “indexes” tab, select “id” field and press the right arrow, it should end up looking something like this:

Now, add the “title” and choose the type “varchar”, like this (length of 100 symbols should be enough for your title, but when you will be adding a field for url you would want to change it to 255):


Finally, click the “Save” icon and you will be asked to name your table. Rails has a special notation for naming tables – basically they have to be in plurals, as they will hold instances of particular class (objects).
Let’s call the table “Stories” (Rails is supposed to know the plural form for “Story”, so let’s try it out). Go back to the main console window and double click the “Tables” subfolder. On the right you will see the list of tables (we have only one at the moment – called “Stories”). Double click the table, it should return an empty set, as you do not have any records yet. (Do not try to create a record – MySQL CC will not correctly do this in MySQL 5.x).
7. Finally, it would be wrong to run your project using a root password. So, you need to create the database user especially for your project. It is very easy to do. In the main console right-click on the “User Administration” folder and select “New User”. Fill out the form as shown:

Username: rails (this would be the username exclusively used for your project)
Host: localhost
Password: password you will use to access this user account
Priviliges: You should provide the least possible priviliges for the user. The ones selected by default are ok, but you can actually safely leave priviliges to only “Select, Insert, Update and Delete”, as we do not plan to create, drop (delete) or alter (change structure of) tables from our Rails scripts. Finally, select, to which databases you are allowing access. Select ONLY the “firstproject” database and click “Add”.
V. Rails in action
1. Now that the database model was prepared, we need to switch it on for our project. Go into your rails project subdirectory (you remember where you created it, right?). In the “config” subfolder you will find file database.yml. Open it and find the “development” section. In it, change database name to “firstproject”, username to “rails”, password to rails user password. Leave socket unchanged. Here is how it will look like:
development:
adapter: mysql
database: firstproject
username: rails
password: railspass55
socket: /path/to/your/mysql.sock
In order for those changes to work, you will need to restart the WEBrick server. Close the WEBrick window and run the server command again:
ruby script/server
2. You have the database schema defined and you configured Rails to access your database. Now, what we need is to create the class called “Story”. The easiest way to go is to create a scaffold, which is basically a default script to work with the standard database operations under the acronym of CRUD (Create Read Update Delete or in SQL terms: INSERT, SELECT, UPDATE and DELETE). To do this, again go to your script’s directory and run this command:
ruby script\generate scaffold Story
Make sure that you use the capital “S” – this is actually a Ruby rule that you need to follow – class names need to be capitalized. So, this is where the magic happens – you do not need to define the class in Ruby the “hard-way”. You define the class variables (so-called states) only once – in the database, and then those variables are automatically recognized by Rails and a standard CRUD framework is applied, which you can replace with your own code as you go.
3. Finally, check that everything works by going to this link: http://127.0.0.1:3000/stories/
For fun, try creating, updating and deleting records and see how the table records are automatically updated in the MySQL CC or Query Browser.

No comments:

Post a Comment