# MySQL - Rails Connection with sample application

**URL:** <https://www.ruby-forum.com/t/mysql-rails-connection-with-sample-application/186068>\
**Category:** Rails\
**Created:** [July 10, 2007, 8:46am UTC](https://www.ruby-forum.com/t/mysql-rails-connection-with-sample-application/186068 "2007-07-10T08:46:07Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![jacobnelson79](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/ad7895/32.png) [@jacobnelson79](https://www.ruby-forum.com/u/jacobnelson79)\
**Post date:** [July 10, 2007, 8:46am UTC](https://www.ruby-forum.com/t/mysql-rails-connection-with-sample-application/186068/1 "2007-07-10T08:46:07Z")

</div>

Step 1: Create a new project named “projectdb” by typing the command  
“rails projectdb”.

Step 2: generate a controller by typing the command “script/generate  
controller projectdb” which will create a file named  
“projectdb\_controller.rb” in “app/controllers”.

open the file “config/database.yml” and set the username and password  
for the database.

create a database named projectdb\_development

type the command “script/generate migration projectdb” or type the  
command “script/generate migration projectdb” which will create a file  
named “001\_create\_projectdbs.rb” in the “db/migrate” folder

open the file “001\_create\_projectdbs.rb” and add the fields you want in  
the MySQL table in the method “self.up”  
eg:  
def self.up  
create\_table :projectdbs do |t|  
t.column :fname, :string  
t.column :lname, :string  
end  
end

run the command “rake migrate” which will create the table “projectdbs”  
with the fields specified in the file “001\_create\_projectdbs.rb”

# Files Required

## projectdb\_controller.rb

class ProjectdbController \< ApplicationController

def list  
@allvars = Projectdb.find(:all)  
end

def new  
@allvars = Projectdb.new  
end

def create  
@allvars = Projectdb.new(params[:fld])  
if @allvars.save  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘new’  
end  
end

def show  
@allvars = Projectdb.find(params[:id])  
end

def edit  
@allvars = Projectdb.find(params[:id])  
end

def update  
@allvars = Projectdb.find(params[:id])  
if @allvars.update\_attributes(params[:fld])  
# flash[:notice] = ‘Entry was successfully updated.’  
redirect\_to :action =\> ‘list’  
else  
render :action =\> ‘edit’  
end  
end

def delete  
Projectdb.find(params[:id]).destroy  
redirect\_to :action =\> ‘list’  
end  
end

## new.rhtml
 MySQL Connection

\<%= start\_form\_tag :action =\> ‘create’, :id =\> @allvars[:id] %\>

| Connect to MySQL |
| 

```
   <tr>
        <td align="right" height="25" width="50">Last

```

Name:&nbsp;&nbsp;

  

| &nbsp; | &nbsp; |
| First Name:&nbsp;&nbsp; | \<%= text\_field("fld", "fname", "size"=\>20) %\> |
| \<%= text\_field(“fld”, “lname”,  
“size”=\>20) %\> |

 |

\<% end\_form\_tag %\>

## list.rhtml
Database Connectivity

Connected

\<% if @allvars.blank? %\>

There are not any ads currently in the system.

\<% else %\> 

These are the current classified ads in our system

\<% @allvars.each do |c| %\> - \<%= link\_to c.fname + ' ' + c.lname, {:action =\> 'show', :id =\> c.id} -%\> \<%= link\_to "Edit", {:action =\> 'edit', :id =\> c.id} -%\> \<%= link\_to "Delete", {:action =\> 'delete', :id =\> c.id}, :confirm =\> "Are you sure you want to delete this item?" %\> 
 \<% end %\> 
\<% end %\> 

\<%= link\_to "Add new", {:action =\> 'new' }%\>

## show.rhtml

**Fname** \<%= @allvars.fname %\>  
**Lname:** \<%= @allvars.lname %\>

* * *
\<%= link\_to 'Back', {:action =\> 'list'} %\>
## edit.rhtml
 MySQL Connection

\<%= start\_form\_tag :action =\> ‘create’, :id =\> @allvars[:id] %\>

| Connect to MySQL |
| 

```
   <tr>
        <td align="right" height="25" width="50">Last

```

Name:&nbsp;&nbsp;

  

| &nbsp; | &nbsp; |
| First Name:&nbsp;&nbsp; | \<%= text\_field("fld", "fname", "value" =\> @allvars[:fname], "size"=\>20) %\> |
| \<%= text\_field(“fld”, “lname”,  
“value” =\> @allvars[:lname], “size”=\>20) %\> |

 |

\<% end\_form\_tag %\>
