| 
 
  
 
Name my buck
I got this buck from my wife for Christmas. We like to shoot at it. 
  
The results so far...
  - Uncle Buck
 
  - Buckerle
 
  - Buckmeister
 
  - default
 
  - Ed 
 
  - reassuringly
 
  - buckminster
 
  - Impossiblebucker
 
  - glowy
 
  - Buckweiser
 
  - Poop McGee
 
  - <a href="http://localhost" onclik="alert('ok)">www.google.com</a>
 
  - http://www.google.com
 
  - @
 
  - [
 
  - ''
 
  - 8
 
  - labo_soanata
 
  - Buck yeah
 
  - Buckington
 
  - Motherbucker
 
 
 
How did that happen? 
First we have a model to store in our database called BuckName. 
It inherits everything from the ActiveRecord class, which has all we need to create objects and save them to the database. 
Ruby code
class BuckName < ActiveRecord::Base
end
  
Now that we have the model, we need a controller to do the magic behind the page. 
The view passes parameters to the controller, which updates the instance variables available in the view logic. 
We can pass the name to the controller, and the controller sends back all your bucks in instance variable @bucks. 
 class BuckController < ApplicationController
  def index
    
	
	name = params[:name]
	
	
        if name and !name.blank?
	  
	  buck = BuckName.find(:first, :conditions => {:name => name})
	  if buck
	    
	    buck.votes += 1
	    buck.save
	  else
	    
	    buck = BuckName.create({:name => name, :votes => 1})
	  end
          
          flash[:notice] = 'You voted for ' + name
	end
	
	
	@bucks = BuckName.find(:all, :order => 'votes DESC')
	
  end
  
  def set_title
    @title = 'Name my buck'
  end
  
end
 
Now we only need to iterate through @bucks, which the controller has carefully sorted by the number of votes.
Ruby has a fun iterator for this, each do  
 @bucks.each do |buck|
  
end  
Rails code
The rails is pretty easy from here, don't you think? 
 <h1>Name my buck</h1>
<p>I got this buck from my wife for Christmas. We like to shoot at it.</p>
<img src="/images/buck.jpg"> </br>
<% form_tag :action => 'index' do %>
  <p>Name my buck
  <%= text_field_tag 'name', @name %>
  <%= submit_tag 'Vote' %></p>
<% end %>
<p><b>
<h2>The results so far...</h2>
<ol>
<% @bucks.each do |buck| %>
  <li><%=h buck.name %></li>
<% end %>
</ol>
</b></p>  
 |