Knowledgebase: Ruby on Rails
Set Up Rails on Shared Hosting
Posted by on 12 July 2013 01:56 AM
This is a guide about how to set up Ruby on Rails, or simply Rails, on your shared hosting account. Be sure to request SSH access so that you may complete these steps.
Note: we use Ruby with Passenger, not Mongrel

Setting It Up

To get started setting it up:

  1. Log in to SSH
  2. Create a new Rails app via SSH
  3. Navigate to your home directory (if you just logged in you are already there!)

    cd ~

     
  4. Create the directory that holds your Rails application
    Note: MyApp can be changed to whatever you would like but it needs to remain the same wherever you type 'myapp'. 
  5. Link your Rails application to your web directory so that your directory has access to your Rails application on your website

    cd www
    ln -s ../myapp/public myapp

     
  6. Let the server know about your Rails application by opening up the file located at myapp/public/.htaccess and adding this as the only thing in the file:

    RailsBaseURI /myapp

    You can access this over FTP or by using the nano or vi commands over SSH. This file shouldn't already exist in the system. If it does go ahead and empty it's contents and add the above.

Create Models, Controllers, Views

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.
Controllers are what moves data between the model (database) and your view. A controller can be created using the following code:

rails generate controller User

Views are where you will control what the user sees. The view can either be created manually or with help of the generator as shown below:
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:
  1. Edit the user controller file found at app/controllers/user_controller.rb with the following code within the class in this file (after class and before end):

    def list
    end

     
  2. Create app/views/user/list.rhtml, which will allow you to view the list action at http://www.example.com/myapp/user/list
  3. Configure your database.yml (for database connections)
    Note: 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: test_databasename
    username: username
    password: password
    hostname: servername

     
  4. Create your first model (this will also generate your migration file)

    script/generate model User
     
  5. Edit the migration for this mode
  6. 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
     
  7. Now create your first controller

    script/generate controller user


  8. You can also predefine the list action. This will create the necessary view as well

    script/generate controller user list


  9. Now set the user controller and list action as your default action. 
    • 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'

You should be all set, now go learn Rails!
(0 vote(s))
This article was helpful
This article was not helpful

Comments (0)
Post a new comment
 
 
Full Name:
Email:
Comments:
Help Desk Software by Kayako fusion