By Hadiyah Mujhid (Co-Founder, Black Founders) This is a tutorial to on how to create a simple Rails 3.2 web app.
- Ruby 1.9.3, RubyGems, and Rails 3.2 installed. If not, visit rubyonrails.org/download
- PostgreSQL is installed. If not, visit postgresql.org/download/
- 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 –database=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. 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
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
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
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.
I’ll add another post soon on how to customize the look of your app by using html and embedded Ruby.
This post was originally posted at Engineers Don't Blog.
Photo credit: Juan G. Hurtado on Flickr About the guest blogger: Hadiyah Mujhid is an entrepreneur and software engineer currently working on early stage startups in San Francisco. She co-runs Black Founders, an organization that promotes diversity in the startup ecosystem. Hadiyah blogs at Engineers Don't Blog. Follow her on Twitter at @hadiyahdotme.