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
---------
<html>
<head>
<title>MySQL Connection</title>
</head>
<body>
<%= start_form_tag :action => ''create'', :id =>
@allvars[:id] %>
<table align="center" border="0"
cellpadding="0" cellspacing="0"
width="300">
<tr>
<td align="left" width="50%">Connect to
MySQL</td>
</tr>
<tr><td>
<table align="center" border="0"
cellpadding="0"
cellspacing="0" width="95%"
bgcolor="#CC6699">
<tr><td height="5"> </td><td
height="5"> </td></tr>
<tr>
<td align="right" height="25"
width="50">First
Name: </td>
<td align="left" width="50"><%=
text_field("fld", "fname",
"size"=>20) %></td>
</tr>
<tr>
<td align="right" height="25"
width="50">Last
Name: </td>
<td align="left" width="50"><%=
text_field("fld", "lname",
"size"=>20) %></td>
</tr>
</table>
</td>
</tr>
</table>
<% end_form_tag %>
</body>
</html>
list.rhtml
----------
<html>
<head><title>Database Connectivity</title></head>
<body>
<p>Connected</p>
<% if @allvars.blank? %>
<p>There are not any ads currently in the system.</p>
<% else %>
<p>These are the current classified ads in our system</p>
<ul id="classifieds">
<% @allvars.each do |c| %>
<li><%= 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?" %>
</li>
<% end %>
</ul>
<% end %>
<p><%= link_to "Add new", {:action =>
''new'' }%></p>
</body>
</html>
show.rhtml
----------
<html>
<body>
<p><strong>Fname </strong><%= @allvars.fname %><br
/>
<strong>Lname: </strong> <%= @allvars.lname %><br />
</p>
<hr />
<%= link_to ''Back'', {:action =>
''list''} %>
</body>
</html>
edit.rhtml
---------
<html>
<head>
<title>MySQL Connection</title>
</head>
<body>
<%= start_form_tag :action => ''create'', :id =>
@allvars[:id] %>
<table align="center" border="0"
cellpadding="0" cellspacing="0"
width="300">
<tr>
<td align="left" width="50%">Connect to
MySQL</td>
</tr>
<tr><td>
<table align="center" border="0"
cellpadding="0"
cellspacing="0" width="95%"
bgcolor="#CC6699">
<tr><td height="5"> </td><td
height="5"> </td></tr>
<tr>
<td align="right" height="25"
width="50">First
Name: </td>
<td align="left" width="50"><%=
text_field("fld", "fname",
"value" => @allvars[:fname], "size"=>20)
%></td>
</tr>
<tr>
<td align="right" height="25"
width="50">Last
Name: </td>
<td align="left" width="50"><%=
text_field("fld", "lname",
"value" => @allvars[:lname], "size"=>20)
%></td>
</tr>
</table>
</td>
</tr>
</table>
<% end_form_tag %>
</body>
</html>
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---