Category Archives: Ruby on Rails

[BlogEntry] Yet Another Web Framework Shoot-Out

This is a really well done, albeit long, screencast comparing JSP, Rails, Zope (Plone), Turbo Gears, Django, and J2EE. It is entertaining, smart, and fun. It's also a great example of quality presentation techniques. And the final score is almost definitely going to surprise you.

It took me over five months to get around to watching this presentation. I recommend you not wait that long. Now, please excuse me while I go take a good look at…

[BlogEntry] NoMethodError in Users#list from Streamlined

I've been playing with Streamlined today, and, despite the never-restart-the-server impression you get from the movie, there are times when you get errors as you add belongs_to, has_many, and relationships to your model and streamlined files.

I pulled my hair out for far too long and was ready to chalk it up to buggy, prerelease code, when I just figured I'd try restarting WEBrick. Sure enough, that was all that was needed.

*sigh*

Streamlined is quite awesome, by the way. It's the missing piece that makes Rails absolutely fantastic. Now, it's a real turn-key application generation platform.

[BlogEntry] Entrepreneurial aspirations drive software innovation

I suspect that a large part of what makes Ruby on Rails so captivating has to do with its promise of rapid web site development driven by entrepreneurial pursuits. (I know that's true for me.) Certainly, that's what motivated the 37signals guys, and look at what they did with it (backpack, basecamp, etc.). Most big and medium-sized companies are deeply entrenched in Java, and their idea of rapid (as in RAD) is far from what the midnight engineer or solo entrepreneur needs. There's a chasm between the needs of a major eCommerce site and that of a small business with a niche product or service to sell. You've only got so many hours of productive coding in you after long hours at your day job. You need genuinely rapid application development.

Code generation has always held great promise, and Rails is a great framework, but there is still a long row to hoe in creating a fully functional site that includes sales, memberships, affiliate links, not to mention all the magic known only to marketing gurus that make a site successful. I've been working for months on an idea that ought to be so easy, but there's so many little details to work out. Every time I think I'm approaching product launch, I turn a corner and spot three more weekend-long things that have to be done. Frankly, I'm exhausted.

It's frustrating when you can visualize the finished product, but it just never seems to get any closer. So, I just found this product that generates turn-key, marketing-savvy, ready-to-launch web sites with a ton of features — the same features I've been coding for months! They're in PHP, not Rails or Java, but I don't care. I've come to realize that I've got to not be so pedantic (read anal 😉 about that. I'm trying to make money, not defend a technology. With this, I can focus on what makes my application unique without spending time reinventing the wheel just because I know how to.

I have found that my success in business has actually been impeded by the fact that I am a software engineer. Because I know how to code this stuff, it doesn't mean that I have to. And yet, I am compelled to do so, rather than download it, buy it, or even outsource it. I'm not sure if it's ego, uncertainty about the process, or just wanting to have things just so, but I'm through with that modus operandi. What about you? Has your ability to write code ever prevented you from working smart, rather than hard?

[BlogEntry] Returning and Executing JavaScript from a Rails URL

AuctionRelay.com has been running in Tomcat for a few years. A simple "include" script tag is all it takes to make AuctionRelay add HTML to the middle of an existing web page. The trick is in using the document.write command to make executable JavaScript write visible HTML at the location in the document where it executes.

I'm now in the process of rewriting AuctionRelay to run on Ruby on Rails (shouldn't that really be Rails on Ruby?). This morning, it was time to tackle the issue of setting the HTTP Content-Type header in the response, so that the browser would recognize that the response was executable JavaScript, and not displayable HTML. (Of course, the executable JavaScript is responsible for squirting in the displayable HTML, but that's the kind of convoluted world we've created for ourselves.)

I can browse to http://localhost:3000/js/test, which includes the line

<script src="http://localhost:3000/js/free?id=option_insight"/>

and see something like this:
in the midst of the original HTML page. (What you see here is still the output of the Java version.)

js_controller.rb:

class JsController < ApplicationController

  def out
    @sellerId = @request.params['id'].to_s
    $mylog.debug("free request for " + @sellerId)
    
    if $freeSellers[@sellerId].nil?
      $mylog.debug("Not found - instantiating...")
      $freeSellers[@sellerId] = Ebay.new
      $freeSellers[@sellerId].retrieve(@sellerId) 
    else
      $mylog.debug("Already exists.")
    end
    
    @headers["Content-Type"] = "text/javascript"
  end

  def test
  end
end

test.rhtml:

Before
<script src="http://localhost:3000/js/out?id=option_insight"/>
After

out.rhtml:

document.write('<table border=1 align=center><caption><br/>My eBay feedback rating is\
<a href="http://feedback.ebay.com/ws/eBayISAPI.dll?ViewFeedback&userid=<%= @sellerId %>" target="_blank">\
<%= $freeSellers[@sellerId].feedback %>\
</a><br/></caption>\
<tr><th>Item<th>Price<th>Bids<th>Ends<tr>\
<td colspan=4 align=center>Display your live auctions on any web site for free at\
<a href="http://www.auctionrelay.com" target="_blank">AuctionRelay.com</a></table>');

One of the things I learned the hard way is that the string you pass to document.write must either be on a single line or use backslashes for continuations. (I was trying to make out.rhtml readable. Duh. Hindsight is 20/15 here.) Fortunately, the Firefox JavaScript Console gave me an error message that made me realize what was going on. Unfortunately, it took me 15 minutes of head-scratching to even think to look at it and recognize that the problem was with the actual JavaScript syntax and not something server-side.

[BlogEntry] Iterating over a Hash in Ruby

Let's say you define a simple Hash.

irb(main):001:0> h1 = {"a"=>1,"b"=>2,"c"=>3}
=> {"a"=>1, "b"=>2, "c"=>3}

What do you think you'll get when you send an each message to it? I figured either a, b, c or 1, 2, 3. Surprise:

irb(main):003:0> h1.each{|a| puts a}
a
1
b
2
c
3
=> {"a"=>1, "b"=>2, "c"=>3}

It all comes out, keys and values interleaved.

Now, the trick becomes figuring out what to Google for to learn the syntax that gets you just the part of the hash you're after. Let me save you a little time. What you're after is each_value.

irb(main):008:0> h1.each_value {|a| puts a}
1
2
3
=> {"a"=>1, "b"=>2, "c"=>3}

You can also use each_key. Fairly intuitive, once you know it. Ah, there's the rub. 🙂

irb(main):009:0> h1.each_key {|a| puts a}
a
b
c
=> {"a"=>1, "b"=>2, "c"=>3}