How do I create a Ruby on Rails application?

Overview

Below are instructions on how to create a Ruby on Rails test application and writing a very basic program.

Creating a Ruby on Rails Test Application

Steps

  1. Click on the icon for "Ruby on Rails"
  2. Check the box for "Load on boot?"
  3. Fill in the "App Name" box and name it "test"
  4. Application Path will be automagically filled in for you as public_html/test
  5. Select "production" for the Environment, which should already be selected as the default.
  6. Click "Create"
  7. To view your new application go to http://www.yourdomain.com/test/public/

You should now see a "Welcome aboard" message which is the default Ruby index page.

If you get a 400 Error, make sure to include the trailing slash "/" after "public" or you may not see the default Rails welcome page.

http://www.yourdomain.com/test/public/ <-- Correct
http://www.yourdomain.com/test/public <-- Incorrect

Now that you have created your RoR framework we will make a very basic RoR program. For this tutorial we will be using Pico, Putty and Windows.

Creating a Very Basic Ruby on Rails Program

Windows: This tutorial assumes it is the OS running on the computer you are viewing this page in.

Putty: The SSH program for connecting to the Linux server from Windows using SSH. If you don't have Putty you can download it herehttp://www.chiark.greenend.org.uk/~sgtatham/putty/download.html .

Pico: A Linux text editor install on the server.

Steps

1. Login to your shell account using Putty (or your favorite SSH program) using your username and password. Once logged in, navigate to your "test" directory that we created in the previous tutorial. 

Command:

 [username@server ~]$ cd public_html/test

2. Execute this command from your Rails application directory ("test") to generate your Rails controller: 

Command:

 [username@server ~]$ ruby script/generate controller hello index

3. Now you need to build your program. You can use notepad or some other text editor, and upload the file, or you can edit it right in your shell using pico.

Command:

 [username@server ~]$ pico app/controllers/hello_controller.rb

You are now using the Pico text editor. You will see something similar to:

Code: 

 class HelloController < ApplicationController

  def index
  end
end

Change this file so that it contains:

Code: 

 class HelloController < ApplicationController

  def world
  @greeting = "hello world!"
  end
end


Press ctrl+x and type "y", then press enter to write the text to the file.

"def world" is a method called whenever users request http://www.yourdomain.com/hello/world

@greeting is a variable, and is also an object. The @ means it will be seen by the code in your application until the end of the request. 

Lastly we need to make the file "world.rhtml" in app/views/hello/world.rhtml by using Pico again:

Command:

 [username@server ~]$ pico app/views/hello/world.rhtml


Add this line of code to it, then save and exit.

Code: 

 <%= @greeting %>


Go back to your control panel, click on Ruby on Rails and make sure your application is running. If it shows as "Not Running", click the "Run" button in the "Actions" column for "Available Ruby on Rails Applications."

Once you have started the application, you can view it on the web:

http://www.yourdomain.com/test/public/hello/world/

  • 0 Utilisateurs l'ont trouvée utile
Cette réponse était-elle pertinente?

Articles connexes

How do I deploy a Ruby on Rails environment?

Overview Ruby on Rails is an open source web application framework for the Ruby programming...

How do I install a Ruby Gem?

Overview Ruby Gems are are collections of functions that allow you to perform tasks in Ruby. You...