Pages

Friday, August 6, 2010

Rails Application with MySQL DB

Follow the following step to work on rails application with mysql.





1:Our system should have the ruby installed .If not install the ruby from here
2: Start a Command prompt with Ruby C:\Ruby\bin
3:Enter the following command to install the special version of MySQL bindings.
      gem install mysql
You should expect a similar output like this:
  Successfully installed mysql-2.8.1.1-x86-mingw32
  1 gem installed
4:Enter the following command to install rails
     gem install rails
This is going to take a bit, since Rails and it’s dependencies takes around 2MB or so, and need to be downloaded and
installed.
Once done, expect see at the screen something like this:
  Successfully installed activSupport-2.3.2
  Successfully installed activerecord-2.3.2
  Successfully installed actionpack-2.3.2
  Successfully installed actionmailer-2.3.2
  Successfully installed activeresource-2.3.2
  Successfully installed rails-2.3.2
  6 gems installed
5:Copy the libmySQL.dll file from here
and paste it in your c:\ruby\bin location.
6:Create a rails application with mysql .Create a new Folder RailsPrg in C:

In Command prompt switch to C:\RailsPrg>
Let’s name our application appInMySQL
      rails appInMySQL --database=mysql
The --database option indicates to Rails that we want to use
MySQL instead of the default database adapter (SQLite3).
Rails will output a lot of lines when creating your application
structure, just an excerpt of what to see:
  ...
   create config/database.yml
   create config/routes.rb
   create config/locales/en.yml
   create config/initializers/backtrace_silencers.rb
   create config/initializers/inflections.rb
   create config/initializers/mime_types.rb
   create config/initializers/new_rails_defaults.rb
   create config/initializers/session_store.rb
   create config/environment.rb
   ...
7:Now Rails have configured for us the name of the database we want to use, and you can verify it in appInMySQL\config\database.yml
In that database.yml file modify as you need in the development phase
development:
   adapter: mysql
   encoding: utf8
   reconnect: false
   database: testMySql
   pool: 5
   username: username
   password: password
   host: hostname
8:In command prompt switch to C:\RailsPrg\appInMySQL> type the command
      rake db:create
Just that, simple db:create is going to connect to our MySQL server, and create the database for us.
9:Let’s verify that everything is in place, using the following command:


     ruby script\about
We can see the something like this as result:
About your application's environment
  Ruby version 1.9.1 (i386-mingw32)
  RubyGems version 1.3.4
  Rack version 1.0 bundled
  Rails version 2.3.2
  Active Record version 2.3.2
  Action Pack version 2.3.2
  Active Resource version 2.3.2
  Action Mailer version 2.3.2
  Active Support version 2.3.2
  Application root C:\RailsPrg\appInMySQL
  Environment development
  Database adapter mysql
  Database schema version 0
10:Now we can start generating the table for our application
Command to create a table & generate the pages for performing operation like inserting, deleting & modifying
Table name is cart
    ruby script\generate scaffold cart cartId:integer productInCart:string
Enter the migrate command
    rake db:migrate
11:Command to start the server ruby script\server
12:Execute http://localhost:3000/carts/ =>You can see the application page & can refine further the application

No comments:

Post a Comment