Such joy from such little code! (again) 7

Posted by brian Monday, January 16, 2006 22:01:10 GMT

One of the best things about Ruby on Rails is the incredible, growing community of extraordinary developers willing to share their work with the world. They allow us all to write really cool pieces of functionality (see my post about flickr.rb) in a really short amount of time. Geoffrey Grosenbach (aka topfunky) is one of these fine folks. In addition to his already great Gruff and Sparklines graphing libraries, he’s just published the first cut of his CSS Graphs Helper, which does some basic graphing using pure CSS (i.e. no images) – inspired by the Apples To Oranges article that some of you might have seen.

So, we decided to take the CSS Graphs Helper and create a really quick and dirty, er, poll/voting thingy, complete with Ajax-y goodness… as shown below.

First, follow the instructions on how to install the CSS Graphs Helper plugin. Then, create a couple of tables to hold the questions and choices.

CREATE TABLE `questions` (
  `id` int(11) NOT NULL auto_increment,
  `text` varchar(200) default NULL,
  PRIMARY KEY  (`id`)
)

CREATE TABLE `choices` (
  `id` int(11) NOT NULL auto_increment,
  `question_id` int(11) default NULL,
  `text` varchar(200) default NULL,
  `votes` int(11) default NULL,
  PRIMARY KEY  (`id`)
)

Then, create two Rails models – Question, which has_many Choices, and Choices, which belongs_to question. (Just saved you about a half hour of stored procedure writing :)

Then, you can create a view:

<div id="poll">
        <%= form_remote_tag :url => { :action => 'vote', :id => @question.id }, :update => 'poll' %>
            <p><%=h @question.text %></p>
            <p>
                <% @question.choices.each do |choice| %>
                    <%= radio_button_tag "answer", choice.id %> <%= choice.text %><br/>
                <% end %>
            </p>
        <%= submit_tag "Vote" %>
        <%= end_form_tag %>
</div>

...and partial (for the AJAX stuff):

<h3><%= question.text %></h3>
<p><%= bar_graph *votes %></p>
<p>
    <% question.choices.each do |choice| %>
        <%= "#{choice.text}: #{choice.votes.to_i} votes.<br/>" %>
    <% end %>
</p>

Finally, a controller:

class PollController < ApplicationController

    def index
        @question = Question.find(@params[:id])
    end

    def vote
        question = Question.find(@params[:id])
        choice = Choice.find(@params[:answer])
        choice.votes = choice.votes.to_i + 1
        choice.save
        votes = question.choices.map { |choice| [choice.text, choice.votes.to_i] }
        render :partial => 'results', :locals => { :question => question, :votes => votes }
    end

end

Obviously, some additional styling and such would be really nice, if you were to actually use this in your web application. And, refactoring this into a helper or component would be cool, so that it could be embedded across different pages or applications. But, the moral of the story is, it took me a heck of lot longer to write this post than it did to actually write the code.

Comments

Leave a response

  1. Peter   January 16, 2006 @ 11:07 PM

    Sick !!!!!

    Don't you just love RoR

  2. JP   January 18, 2006 @ 10:08 PM

    That's pretty nice. Quick question for you guys though...

    I'm primarily an ASP.net 1.0 developer. Haven't looked at 2.0 much, but about to.

    Done some rails and loved it. What do you guys think of ASP.net 2.0? I'd be interested in your thoughts, especially if you've looked at it.

  3. Brian   January 18, 2006 @ 11:21 PM

    JP: A very good question. Admittedly, I haven't had much of a chance to actually build a production site using ASP 2.0 - pretty much just played around with it. A couple of things that are vastly improved: obviously the C# 2.0 language with support for generics, anonymous methods, etc. is nice; master pages provide *similar* functionality to Rails' layouts, and adherance to web standards is much improved.

    But overall, having developed more than a few ASP/ASP.NET 1.1 sites, it is still (to me, at least) a strange mashup of technologies that try desparately to be all things for all types of developers. (e.g. trying to make web sites drag-and-drop, server controls, postback, etc.) Trying to make web development an easy task for, say, a Windows client developer, isn't a good thing, IMO. Rails is much different. It is exclusively a web development platform. It encourages you to think about good application design (e.g. MVC), web standards (CSS/XHTML), and stuff like that.

    Anyway, thanks for raising the question. I'll begrudgingly investigate ASP 2.0 a little further, so we can post a longer discussion on it later.

  4. Cheap tramadol   April 24, 2006 @ 11:44 PM

    good article, it'd be nice if more of the headers linked off to more articles.

  5. Gran Stripping   April 28, 2006 @ 09:01 AM

    Well done, nice instructions :)

  6. vemxnbluzv   July 06, 2007 @ 09:39 PM

    Hello! Good Site! Thanks you! dyzpgiwiaeloqw

  7. dsemzvmtll   November 26, 2007 @ 01:53 AM

    24 Connecticut upset fifth ranked Stanford 2 0, eliminating the host Cardinal from the NCAA women's soccer tournament. The Cardinal (15 3 5) were looking to advance to the final eight of the tournament, but instead the Huskies (14 5 2) will be playing for a spot in the final four. They will face Florida State next weekend. The Cardinal made a second Sweet 16 in a row. The six Cardinal seniors depart after compiling 53 wins in their four seasons on the Farm. "It was just a weird game," said Stanford coach Paul Ratcliffe, whose team outshot UConn 12 7. "I felt like we dominated the first half and Connecticut just was able to convert (its) two scoring chances. "I am so proud of the players (who) are moving on from this program, they are such great people." Men's soccer: Andrew Wiedeman scored off a...
    http://news-blog-for-you.com/

Comment


(won't be published)