Knowledgebase: Ruby on Rails
Setup Rails
Posted by on 11 January 2013 04:32 PM
Here's a guide on how to setup a new rails app on shared hosting.

1. Create a new Rails app via SSH 
If you don't have SSH access you will need to fill out a support ticket with HostNine to gain access.

A) Move into your home directory. (If you just logged in, you are already there.)
cd ~

B) Create the directory that holds your Rails application. (myapp can be changed to whatever you would like, but whatever you choose needs to stay the same wherever you type myapp.)
rails new myapp
C) Link your rails application into your web directory. This lets you access your Rails application on your website.
cd www
ln -s ../myapp/public myapp

D) Let the server know about your Rails application. Open up the file located at myapp/public/.htaccess. You can access this over FTP or using the nano or vi commands over SSH. This file shouldn't already exist in the system. If it does, you can empty it's contents. Put this as the entire contents of the file:
  RailsBaseURI /myapp

E) From here you will create your needed models, controllers, and views for your application. Models are the objects, which will usually be linked to a table in your database. A model can be created used the following command:
cd ../myapp
rails generate model User
(Note the name of the model is capitalized and singular.)
F-1) A controller is what moves data between the model (database) and your view. A controller can be created using the following code.
rails generate controller User
(Again, note the capitalization and singularity.)

F-2) The view is where you will control what the user sees. The view can either be created manually or with help of the generator.
rails generate controller User list
The above code would create the controller for the User object and it will also create the needed code for the list view. If you would like to manually create this. All you do is edit the User controller file which is found at app/controllers/user_controller.rb. In this file, enter the following code within the class. (After class and before end)
def list
end

G) Then, create app/views/user/list.rhtml. This will then allow you to view the list action at http://www.example.com/myapp/user/list

2. Configure your database.yml (for database connections)
We are using mysql in this example
vim config/database.yml


development:
  adapter: mysql
  encoding: utf8
  database: databasename
  username: username
  password: password
  hostname: servername

test:
  adapter: mysql
  encoding: utf8
  database: test_databasename
  username: username
  password: password
  hostname: servername

production:
  adapter: mysql
  encoding: utf8
  database: prod_databasename
  username: username
  password: password
  hostname: servername


3. Create your first model (this will also generate your migration file)
Pay attention to capitalization and singularity
script/generate model User


4. Edit the migration for this mode
vim db/migrate/migrationname.rb

class CreateUsers < ActiveRecord::Migration
  def self.up
	create_table "users", :force => true do |t|
	  t.column :login,                     :string
	  t.column :email,                     :string
	  t.column :created_at,                :datetime
	  t.column :updated_at,                :datetime
	end
  end

  def self.down
	drop_table "users"
  end
end


5. Attempt to migrate your code (create the tables in the database
rake db:migrate
6. Now create your first controller
script/generate controller user


You can also predefine the list action. This will create the necessary view as well
script/generate controller user list


7. Now set the user controller and list action as your default action. 
First remove public/index.html

Uncomment the map.root line in routes.rb


vim config/routes.rb

  # You can have the root of your site routed with map.root -- just remember to delete public/index.html.
  map.root :controller => "user", :action => "list

  # See how all your routes lay out with "rake routes"

  # Install the default routes as the lowest priority.
  map.connect ':controller/:action/:id'
  map.connect ':controller/:action/:id.:format'


8. You should be all set, now go learn Rails!

Help Desk Software by Kayako fusion