Rails Web App: Easy Like Sunday Morning

This is a tutorial to on how to create a simple Rails 3.2 web app.

Basic Assumptions:
1. Ruby 1.9.3, RubyGems, and Rails 3.2 installed. If not, visit http://rubyonrails.org/download
2. PostgreSQL is installed. If not, visit http://www.postgresql.org/download/.
3. You are familiar with the Ruby programming language. If not, check out tryruby.org, to learn some basics.

From your terminal window, type the below command line

*mywebapp is used as an example in this tutorial, any name can be used

> rails new mywebapp -d postgresql 

The above line creates the standard rails project directory structure for your app. By default, rails uses sqlite as a database. In the above line, I’ve force rails to use PostgreSQL as its database. This because I plan to hosting the web project on heroku.com. Heroku uses postgresql as a default db. It does not support sqlite or mysql. As a general practice, it is best to use the same db for both your development and production.

Enter the directory created by rails

>cd mywebapp

For the purposes of the this tutorial, edit your database.yml file located in the config directory. Remove the username: entry from each section.

Return to your terminal

> rake db:create 

If you start, your web server. You will see a default Welcome Aboard Message. Start your web server using the below command.

> rails server

Go to your web browser and enter http://localhost:3000

You should see the Welcome Abroad default Message. You can now close your browser and return to the terminal. Press control + c to stop the web server.

Return to the terminal to delete the default Welcome message and create your db

> rm public/index.html

So, let’s say that our web app is a Winery directory. We could create the directory using rails scaffolding. For each winery in our directory, we would need

-Name of the Winery
-Street Address

From the command line type

> rails generate scaffold Winery name:string street_address:string city:string state:string website:string

Now make your changes to the database using the below command. This will create the table Winery

>rake db:migrate

In your text editor, add the below line to the routes.rb file in the config directory. Add the line anywhere in the file between the ‘do’ and before the ‘end’

root :to => “wineries#index”

Return to the terminal and restart your web server

>rails server

Open your web browser and go to http://localhost.com:3000

Congrats you’ve just created a web app using Ruby on Rails. Explore your app by creating entries, then editing and deleting them.

If you’re interested in deploying your app to the internet for all to see, check out this Heroku tutorial http://railsapps.github.com/rails-heroku-tutorial.html

I’ll add another post soon on how to customize the look of your app by using html and embedded Ruby.

One thought on “Rails Web App: Easy Like Sunday Morning

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

About Hadiyah

Practicing loving God, neighbors, and myself daily. Leveraging venture capital to advance racial equity at HBCUvc.