ok, hopefully this will get read by more then one person, just to showcase the crazyness of ruby development... lets build a small application... say starting points of a shopping cart...
Ready, set, go.
ok, start:
>rails cart
mmm, file management
lets go into cart/db and create a new sql file
ok database:
Code:
drop database cart_development;
create database cart_development;
drop database cart_testing;
create database cart_testing;
drop database cart_production;
create database cart_production;
grant SELECT,INSERT,UPDATE,DELETE,CREATE,DROP on cart_development.* to 'user'@'localhost';
grant SELECT,INSERT,UPDATE,DELETE,CREATE,DROP on cart_testing.* to 'user'@'localhost';
grant SELECT,INSERT,UPDATE,DELETE,CREATE,DROP on cart_production.* to 'user'@'localhost';
use depot_development;
create table products(
id int not null auto_increment,
title varchar(100) not null,
description text not null,
image_url varchar(200) not null,
price decimal(10,2) not null,
primary key (id)
)
looks simple enough, now run it:
>mysql -u root -p <db_create.sql
time to point ruby to the databases:
the file is located in the config directory generated under the cart folder
so open up and edit database.yml:
Code:
# MySQL (default setup). Versions 4.1 and 5.0 are recommended.
#
# Install the MySQL driver:
# gem install mysqlqw
# On MacOS X:
# gem install mysql -- --include=/usr/local/lib
# On Windows:
# There is no gem for Windows. Install mysql.so from RubyForApache.
# http://rubyforge.org/projects/rubyforapache
#
# And be sure to use new-style password hashing:
# http://dev.mysql.com/doc/refman/5.0/en/old-client.html
development:
adapter: mysql
database: cart_development
username: user
password:
host: localhost
# Warning: The database defined as 'test' will be erased and
# re-generated from your development database when you run 'rake'.
# Do not set this db to the same as development or production.
test:
adapter: mysql
database: cart_test
username: user
password:
host: localhost
production:
adapter: mysql
database: cart_production
username: user
password:
host: localhost
now that was simple, what is next?
generate the admin environment...
>ruby script/generate scaffold Product Admin
and what does that do? lets just say, that this has just generated a basic admin interface to our database... and you have written no code...
hmm, lets test this
> ruby script/server
that starts up a simple server on the port 3000
test this:
http://localhost:3000/admin
click on add new item... mmm, adminilicious
ok, now this wont actually validate input, but its a start.
now lets add data validation...
we edit app/models/product.rb
Code:
class Product < ActiveRecord::Base
validates_presence_of :title, :description, :image_url #make sure that fields arent empty
validates_uniqueness_of :title #make sure title is unique
validates_numericality_of :price #make sure price is a number
#check image_url for image file extension
validates_format_of :image_url,
:with => %r{\.(gif|jpg|png|jpeg|tiff)$}i,
:message => "You must include a gif/jpg/png/jpeg/tiff file"
protected
#this will generate a pretty error message if price is not a positive integer
def validate
if price.nil? || price >= 0.01
errors.add(:price, "price needs to be more then 0(zero)")
end
end
end
now, there is error checking as well...
hmm, next...
perhaps we want a client side of this app as well...
>ruby script/generate controller Store index
well, if we go to the store, it has a link to index... but there is no index because we have not created it, so lets go and create index in store_controller.rb
Code:
def index
@products=Product.show_items
end
but we have no show_items so
Code:
def self.show_items
find( :all,
:order => " title desc")
end
hmm, perhaps we should pretty it up a bit, no?
all we have to do is go into app/views/store/index.rhtml
Code:
<h1>Product Listing</h1>
<table cellpadding="5" cellspacing="0">
<%
even_odd=0
for product in @products
even_odd= 1 - even_odd
%>
<tr valign="top" class="ListLine<%= even_odd %>">
<td>
<img src="<%= product.image_url%>">
</td>
<td width="450">
<span class="ListTitle">
<% h(product.title) %>
</span>
<br />
<small>
<%= product.description %>
</small>
<br />
<strong>$<%= sprintf("%0.2f", product.price) %></strong>
<%= link_to 'Add to Cart',
:action => 'add_to_cart',
:id => product %>
<br />
</td>
</tr>
<tr><td colspan="2"><hr/></td></tr>
<% end %>
</table>
now, we need a stylesheet... public/stylesheet
Code:
.ListTitle {
color: #ffffff;
font-weight: bold;
font-size:larger;
}
.ListLine0{
background: #e0f8f8;
}
.ListLine1{
background: #f8b0f8;
}
there there, now refresh, and magic.... (this will be in app/views/store/index.rhtml)
wont go into much more functionality... but tell me if there is any other language that upon writing under 100 lines of code you can have a database, admin interface with error checking to it and a customer view of the database?
aint Ruby cool now?