Rails - MySQL Connection with sample application

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:  


   
First Name:   <%= 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:  


   
First Name:   <%= text_field("fld", "fname", "value" => @allvars[:fname], "size"=>20) %>
<%= text_field(“fld”, “lname”,
“value” => @allvars[:lname], “size”=>20) %>

<% end_form_tag %>