Setting up an application with mysql database on rails server on ubuntu 14.04

Simple steps to be followed
I assume you have installed rails, ruby gems and mysql

1) Create a new application with mysql database.

— name of application : myapplication
— db driver mysql

	$ rails new myapplication -d mysql
        

2) Run your application on rails server

CD to myapplication

	$ rails server
       

3) Create a database for the application
CD to myapplication

	$ rails db
        

Enter the password for root user

	mysql> create database mydb;
	mysql> create user abc identified by 'PASSWORD';
	mysql> grant all privileges on mydb.* to abc@localhost identified by 'PASSWORD';
	mysql>quit
	

4) Now place the username,password and database name in database.yml file under config folder of your app.
5) Goto routes.rb
— uncomment root ‘welcome#index’

6) Create a controller class welcome_controller.rb under controller folder and add an action index to it.

	class WelcomeController < ApplicationController
		def index
		end
	end
        

7) Create a view file for function index under views/welcome/index.html.erb
write

<h1>Hello Rails</h1>

to view file.
8) Now run the rails server as
CD to myapplication

   $ rails server
  

open in browser —> localhost:3000

You will see Hello Rails in browser window.

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s