To start a project in Ruby on Rails, you need to login first to your account via ssh. Once you are logged in, let's see where we are located:
Ok, we are in our home directory (otherwise cd ~). Let us start a project called guestbook.[~]# pwd
/home/masood
Now we create a symbolic link to the public directory of our guestbook application from our web root:[~]# rails guestbook
create
create app/controllers
create app/helpers
create app/models
create app/views/layouts
create config/environments
create components
create db
create doc
create lib
create lib/tasks
create log
create public/images
create public/javascripts
create public/stylesheets
create script/performance
create script/process
create test/fixtures
create test/functional
create test/mocks/development
create test/mocks/test
create test/unit
create vendor
create vendor/plugins
create Rakefile
create README
create app/controllers/application.rb
create app/helpers/application_helper.rb
create test/test_helper.rb
create config/database.yml
create config/routes.rb
create public/.htaccess
create config/boot.rb
create config/environment.rb
create config/environments/production.rb
create config/environments/development.rb
create config/environments/test.rb
create script/about
create script/breakpointer
create script/console
create script/destroy
create script/generate
create script/performance/benchmarker
create script/performance/profiler
create script/process/reaper
create script/process/spawner
create script/process/spinner
create script/runner
create script/server
create script/plugin
create public/dispatch.rb
create public/dispatch.cgi
create public/dispatch.fcgi
create public/404.html
create public/500.html
create public/index.html
create public/favicon.ico
create public/robots.txt
create public/images/rails.png
create public/javascripts/prototype.js
create public/javascripts/effects.js
create public/javascripts/dragdrop.js
create public/javascripts/controls.js
create doc/README_FOR_APP
create log/server.log
create log/production.log
create log/development.log
create log/test.log
Now let us see what we have on our site by visiting http://www.yourdomain.com/guestbook/Code:[~]# cd public_html/ [~/public_html]# ln -s ../guestbook/public guestbook
NOTE: Replace "yourdomain.com" with your account domain.You can not access it from server IP/domain or temporary URL. You need to access it using your domain name.
Most of the information on the default index.html is not for you, so you can ignore it.However, if you are an advanced Ruby on Rails programmer, you may probably tweak something on your own.
Let us now generate a controller for our guestbook application. Say MyTest.
Now visit http://www.yourdomain.com/guestbook/MyTestCode:[~]# cd ~/guestbook [~/guestbook]# ruby script/generate controller MyTest exists app/controllers/ exists app/helpers/ create app/views/my_test exists test/functional/ create app/controllers/my_test_controller.rb create test/functional/my_test_controller_test.rb create app/helpers/my_test_helper.rb
That would bring result
Rails tried to find a default action named index in this controller but we have not defined it yet. Let us do that:Unknown action
No action responded to index
The file will have this:Code:[~/guestbook]# vi app/controllers/my_test_controller.rb
and we add these bold lines in between:Code:class MyTestController < ApplicationController end
Save and quit, and then refresh the browser. Viola! you can see the new index method. You can also access it using http://www.yourdomain.com/guestbook/MyTest/indexCode:class MyTestController < ApplicationController def index render_text "Hello World" end end
Let us define another method called jaguarpc
and access it using http://www.yourdomain.com/guestbook/MyTest/jaguarpcCode:def jaguarpc render_text "Jaguar Technologies provides high quality, reliable hosting solutions at prices anyone can afford." end
Now we get back to our guestbook application. The above was just to get you started on rails. First we need to create a database for the guestbook. Please login to your control panel:
http://www.yourdomain.com:2082/
and create the database, its user/pass and grant privileges to the user over the db. Please note down the db name, db user and its password.
Now to tell the Rails to find this database, we edit the file:
In that file we need to define three db connections for three level of dbs. For now we use same connections for all three:[~/guestbook]# vi config/database.yml
Rails lets you run in development mode, test mode, or production mode, using different databases. This application uses the same database for each.Code:development: adapter: mysql database: USER_db host: localhost username: USER_dbuser password: **** test: adapter: mysql database: USER_db host: localhost username: USER_dbuser password: **** production: adapter: mysql database: USER_db host: localhost username: USER_dbuser password: ****
Our application will hold guestbook entries, so let us create a table to hold them. Please visit phpMyAdmin from your control panel and create a table called "entries".
Now we need to create an Entry model class that will hold data from the entries table in the database.Code:CREATE TABLE `entries` ( `id` INT NOT NULL AUTO_INCREMENT , `name` VARCHAR( 50 ) NOT NULL , `comments` TEXT NOT NULL , `date` DATE NOT NULL , PRIMARY KEY ( `id` ) ) TYPE = MYISAM ;
Now if you see the model, it is an empty class:Code:[~/guestbook]# ruby script/generate model Entry exists app/models/ exists test/unit/ exists test/fixtures/ create app/models/entry.rb create test/unit/entry_test.rb create test/fixtures/entries.yml
This empty class definition is the entry business object that Rails maps to the entries table (Rails is pretty smart in English language to figure that out). Let us add controller for it:Code:[~/guestbook]# cat app/models/entry.rb class Entry < ActiveRecord::Base end
Let us add some functionality to it:Code:[~/guestbook]# ruby script/generate controller Entry exists app/controllers/ exists app/helpers/ create app/views/entry exists test/functional/ create app/controllers/entry_controller.rb create test/functional/entry_controller_test.rb create app/helpers/entry_helper.rb
This single line of code will bring the database table to life. It defines actions for all CRUD operations, immediately allowing us to create, read, update, and delete entries in our database! Now visit http://www.yourdomain.com/guestbook/entry and enjoy!Code:[~/guestbook]# vi app/controllers/entry_controller.rb class EntryController < ApplicationController scaffold :entry end
This small tutorial was based on "Rolling with Ruby on Rails" at onlamp.com: http://www.onlamp.com/pub/a/onlamp/2.../20/rails.html to get you started on JaguarPC hosting servers. The tutorial on onlamp is based on windows and WEBrick web server. You can ignore those and read on further on that tutorial and other tutorials:
Rolling with Ruby on Rails (Part 1)
Rolling with Ruby on Rails (Part 2)
Ajax on Rails
Also check out www.rubyonrails.com for more information.


LinkBack URL
About LinkBacks
However, if you are an advanced Ruby on Rails programmer, you may probably tweak something on your own. 



Reply With Quote

Bookmarks