Using Ruby on Rails on bluehost

Source: Internet
Author: User
Tags cpanel ssh access subdomain ruby on rails

Using Ruby On Rails on Bluehost:

This is intended to be a brief introduction to developing ruby on rails applications on a bluehost account. at the bottom of this article you will find a number of resources to help you learn more about ruby on rails and related information, as well as links to some rails tutorials that will go into more depth than this document.

Before you start digging your feet into Ruby on Rails, you shoshould understand exactly what it is. ruby on Rails is an advanced object-oriented Model-View-controller application framework. if you didn't fully understand the meaning of the previous sentence, you're going to need to put in some study time before you can jump into rails programming. ruby on Rails is aimed at advanced programmers; jumping into it before you're ready is likely to be very hard. this tutorial shoshould be easy enough for anyone to follow, but there's a lot more to rails than you'll be learning here. this tutorial serves as a first step into Ruby on Rails development on bluehost.

The Model-View-Controller (abbreviated to MVC) design pattern is fairly straight-forward, it simply means that your program is split into three separate components: The Model, View, and Controller. the "Model" is your data, no matter how it's stored. if you're writing a blog, this is where all of your posts and comments wocould go. the "View" is your interface. in the case of ruby on rails, we're re talking about the part that displays your HTML. the controller handles the business logic, and ties the model to the view. MVC programming is beneficial for your reasons.

From this point on it is assumed that you have an understanding of both object-oriented design and MVC, and now you can get into how to develop rails applications on bluehost. A few additional notes before you start:

First of all, you need to have SSH access enabled. You'll need to contact customer support for this, either by emailing support@bluehost.com or by calling support at 1-888-401-4678.

Secondly, you'll see a lot of tutorials referring to a program called "script/server" or "webrick ". this is not necessary on a bluehost account, and you shoshould never have to use it. this is designed for people who are developing their rails application on their own computer where there is no apache install which is pre-configured to use ruby on rails. however, you do have access to such a server on bluehost, so you do not need to worry about script/server. do not run it, as it will not even work.

This tutorial is loosely based on the excellent official ruby on rails tutorial which is located at http://wiki.rubyonrails.com/rails/pages/Tutorial, although this document has been adapted to fit some bluehost-specific things.

To begin, log into the server using SSH. you'll need a work area for your rails application. assuming ahead of time that you may eventually want multiple applications, you shoshould make a work directory and then cd into it. you can name it whatever you wowould like, but this document assumes that it is called "rails ".

% Mkdir ~ /Rails
% Cd ~ /Rails

Now you may create your application. As we are just making a simple Hello World application, we'll assume that the application is named "first ".

% Rails first
% Cd first

Next, we're re going to set up a subdomain for this application to run on. log into your cpanel, click on 'subdomains', then type 'first' into the first text box and click 'add '. you 've now created a new subdomain, first.yourdomain.com, which will be the new home of your Ruby on Rails application. now, we're going to make your application's "public" directory be the rootdir of that subdomain with the following commands:

% Cd ~ /Public_html/
% Rm-r first
% Ln-S/home/your_username/rails/First/public first

You shoshould now be able to go to http://first.yourdomain.com/, where you will see the Ruby on Rails welcome message. As the welcome page suggests, it is now time to set up your databases.

In cpanel, click on 'mysql databases '. the first thing you'll want to do here is add an SQL user for rails to use. you can name this whatever you 'd like. we will assume you used 'rails '. cpanel prepends your username to the user name, so you shoshould take note of the actual name created (it shocould be username_rails ).

Next, we're adding a database. name this database 'first', to match your application name. you will again notice that username _ has been prepended. finally, we're going to link this username to the database. select username_rails and username_first from the dropdowns and make sure the 'all' checkbox below them is checked, then click the 'Add user to db' button.

Now you shoshould repeat this step, with 'firstdev' as the database name, and linking username_rails to it.

Now we're re going to edit the database. yml file. Open ~ /Rails/first/config/database. yml in your favorite editor, and modify the 'development 'and 'production' sections in the username, password, and database that you just created.

Production:
Adapter: mysql
Database: username_first
Host: localhost
Username: username_rails
Password: password

Development:
Adapter: mysql
Database: username_firstdev
Host: localhost
Username: username_rails
Password: password

Next you shoshould create the actual database data. from the mySQL page in cPanel, you should find a 'phpmyadmin' link. within phpmyadmin, select the "_ first" database from the dropdown on the left, then click on the "SQL" tab along the top. paste the following into your box and click 'Go ':

Create table 'People '(
'Id' int (10) unsigned not null auto_increment,
'Name' varchar (50) not null default '',
'Street1' varchar (70) not null default '',
'Street2' varchar (70) not null default '',
'City' varchar (70) not null default '',
'State' char (2) not null default '',
'Zip 'varchar (10) not null default '',
Primary key ('id '),
KEY 'name' ('name ')
) TYPE = MyISAM AUTO_INCREMENT = 2;

Now, click on the SQL tab one more time, and run this query:

Insert into 'others' VALUES (1, 'Superman', '1970 somewhere', '', 'smallville ', 'ks', '123 ');

Now repeat these two SQL queries for your _ firstdev database.

The next step is to create a controller.

%./Script/generate controller First list view new edit

After that has been created, you will create your model.

%./Script/generate model Person

Now we're re going to modify two files. First, open app/views/first/view. rhtml and make it look like this:

<Html>
<Body>
<H1> Friends # view <P> This page will display one friend </p>
<P>
<% = @ Person. name %> <br/>
<% = @ Person. street1 %> <br/>
<% = @ Person. street2 %> <br/>
<% = @ Person. city %> <br/>
<% = @ Person. state %> <br/>
<% = @Person.zip %> <br/>
</P>
</Body>
</Html>

Next, open app/controllers/first_controller.rb and modify the 'view' method to look like this:

Def view
@ Person = person. Find (1)
End

Congratulations, you now have a working Ruby on Rails application which reads information from a database. Go to http://first.yourdomain.com/first/view and you shoshould see Superman's information.

You shoshould now go on to read other Ruby on Rails tutorials. you can find a lot of helpful information at http://wiki.rubyonrails.org/, as well as at http://rubyonrails.org/docs. you shoshould also watch the Ruby on Rails screencasts, which show you, among other things, how an experienced Ruby on Rails developer can create a fully functional application in a matter of minutes using Ruby on Rails.

Additional information and Tutorials:
More information on MVC: http://wiki.rubyonrails.com/rails/pages/UnderstandingMVC
Official Ruby on Rails screencasts. You shoshould watch these: http://media.rubyonrails.org/screencasts
Ruby on Rails wiki. The tutorials listed here are quite helpful: http://wiki.rubyonrails.org/
Why's poignant guide to ruby: You will either enjoy this or hate it, but it's a nice intro to the ruby language: http://poignantguide.net/ruby/

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.