Episode 43: AJAX with RJS

Posted about 7 years back at Railscasts

This episode will walk you through adding AJAX functionality to a form using RJS. See how to easily update multiple elements on a page.

JRuby / Goldspike / Glassfish Deployment Diary

Posted about 7 years back at zerosum dirt(nap) - Home

Robert Dempsey has written a pretty solid little tutorial on deploying your first JRuby on Rails app with Glassfish. It’s powerful stuff. Go read it now, damnit!

The process can still be a bit tricky the first time, especially if you have additional gem dependencies, etc. But once you get it running you’ll be blown away by how simple it is to create a .war and deploy it to any of numerous pre-existing Java application servers (Glassfish).

I had a few issues initially (particularly with openssl support), so I figured I’d document them as an addenum to Robert’s tutorial in case you’re interested. Read on to see my notes…

  • Make sure to set JRUBY_HOME in your environment. It’s used by Goldspike.
  • If both ruby and jruby are in your path, you can specify the ‘version’ of rake to use by doing: jruby -S rake. This will run the specific command in the JRUBY_HOME/bin directory.
  • Edit the goldspike lib/war_config.rb and change the line that reference jruby-complete version 0.99 to read:
add_java_library(maven_library ('org.jruby', 'jruby-complete', '1.0'))
  • This will look for JRuby 1.0 instead of 0.9.9. As of this writing you’ll also have to manually retrieve jruby-complete-1.0.jar as the remote sources don’t seem to have it yet. Put it in JRUBY_HOME/lib.
  • I happen to be using SSL in my app, so I had to gem install it into the JRuby environment using jruby -S gem install jruby-openssl Make sure the gem ends up in your JRUBY_HOME/lib/ruby/gems hierarchy. Tell goldspike it needs to add this gem with the following line: add_gem(‘jruby-openssl’)
  • If you’re doing the jruby-openssl thing you’ll also have to retrieve the latest version of the Bouncy Castle Crypto APIs package for whatever version of Java you’re using (I’m on OS X, Java 1.5). Put this in your JRUBY_HOME/lib directory and then add the library to your war_config.rb file:
add_java_library(maven_library('bouncycastle', 'bcprov-jdk15', '136'))

UPDATES:

  • Make sure to copy jdbc_databases.rake from ActiveRecord-JDBC to your lib/tasks directory (for ActiveRecord-related rake tasks). Thanks to Ola Bini for pointing me in the right direction there.
  • Include the following in environment.rb:
if RUBY_PLATFORM =~ /java/
  require 'rubygems'
  RAILS_CONNECTION_ADAPTERS = %w(jdbc)
end

First hours with Flex 3 / AIR

Posted about 7 years back at work.rowanhick.com

Small step for man, big step for RIA-kind ! I had to stay up past midnight to be one of the questionably committed few to read the words of joy spread by Ted Patrick. Not more than 2 seconds later I download Flex 3 builder. So far I've managed to spend a good solid 3-4 hrs in Builder 3 without any hiccups or crashes. A little gem of a resource, missed out on the various announcements, are the videos on onflex.org. Check out in particular the refactoring one. Looks like some serious goodness to be toyed with there. Something tells me Adobe is on a war path with this one, going for bigtime developer buy in and I'm pretty much sold. Good stuff Adobe! Top marks.

Why SEO is Important

Posted about 7 years back at PJ Hyett

While I was at CNET, traffic was growing steadily on our sites, but we had a rude awakening one particular day.

The “Oh Shit” moment

I remember the morning when my boss said to me:

We’re having a conference call in a few minutes with CNET’s SEO dude, we lost half of our uniques starting last Thursday, why don’t you join in.

The first thing I did is wonder what the hell I did wrong, because in the back of my mind, this problem was inevitably engineering’s fault.

The realization that Google owns you

Chowhound was getting 85% of its traffic from Google. It has around 500k topics, averaging 4.5 posts per topic, each an opportunity to become indexed by Google.

This particular week, Google decided to purge its index of stale data, and in doing so, eliminated the majority of the pages Chowhound had indexed, thus slashing our uniques in half. An interesting side-note was that our pageviews didn’t drop nearly as much, because the core users that were still visiting the site had a much higher pageview per visit ratio than the Google visitors.

What we did wrong

The solution was actually quite simple. On every board, we originally used ‘Next’ and ‘Previous’ links that would paginate you to the corresponding page of topics. Straightforward, but horrible for Google’s bots. They’d click a few of the Next links and say the hell with this and move on.

To give you an idea, the Manhattan board alone has over 800 pages of topics, and when Google is only looking at the first three pages, you have a massive problem.

How we fixed it

Simply put, I wrote the will_paginate plugin. Instead of just having ‘Next’ and ‘Previous’, it also displays a window of page numbers that allows bots the ability to navigate hundreds more pages within one click. Now we’re getting somewhere.

Eureka!

As the traffic began to come back in a serious way, well beyond where it was before the dip, the notion of SEO became quite clear to me. Given a site with tons of great content, instead of building more features, you should be trying to leverage what you already have.

This also makes a lot of sense from a financial standpoint. How long would it take your team to build a new feature for 10k more uniques a day, how long would it take them to add meta tags and a sitemap for 10k more uniques a day. There’s no comparison.

The Checklist

SEO isn’t rocket science (unless you’re trying to push the envelope), you just have to be aware of a few guidelines.

Page-specific
  • Unique title tag
  • Unique meta description (Yahoo is the only one using meta keywords anymore)
  • One h1 tag (the text used in your h2-h4 tags is still very important, though)
Site-wide
  • Allow only quality outbound links (eg. rel="nofollow" on all user-generated links)
  • Block duplicate content from being indexed with robots.txt
  • Structure your CSS so the content can go first
  • Consistent navigation that allows bots to get to any page on your site within 2 or 3 clicks

Piece of cake. This discussion can go far deeper than these points, but if you do the stuff above, you’re 90% of the way there. The last 10% is the most time-consuming, so I leave that to you to decide if it’s worth your time.

I know a lot of people think of SEO as a dirty word, and that it conjures up negatives connotations of link-baiting and spam, but that really couldn’t be further from the truth. I’m just going to start calling it FT…Free Traffic.

Speaking of Which

Err Free does more than just Rails, we’d be happy to apply what we’ve learned about SEO to your site. Who doesn’t love Free Traffic?

Why SEO is Important

Posted about 7 years back at PJ Hyett

While I was at CNET, traffic was growing steadily on our sites, but we had a rude awakening one particular day.

The “Oh Shit” moment

I remember the morning when my boss said to me:

We’re having a conference call in a few minutes with CNET’s SEO dude, we lost half of our uniques starting last Thursday, why don’t you join in.

The first thing I did is wonder what the hell I did wrong, because in the back of my mind, this problem was inevitably engineering’s fault.

The realization that Google owns you

Chowhound was getting 85% of its traffic from Google. It has around 500k topics, averaging 4.5 posts per topic, each an opportunity to become indexed by Google.

This particular week, Google decided to purge its index of stale data, and in doing so, eliminated the majority of the pages Chowhound had indexed, thus slashing our uniques in half. An interesting side-note was that our pageviews didn’t drop nearly as much, because the core users that were still visiting the site had a much higher pageview per visit ratio than the Google visitors.

What we did wrong

The solution was actually quite simple. On every board, we originally used ‘Next’ and ‘Previous’ links that would paginate you to the corresponding page of topics. Straightforward, but horrible for Google’s bots. They’d click a few of the Next links and say the hell with this and move on.

To give you an idea, the Manhattan board alone has over 800 pages of topics, and when Google is only looking at the first three pages, you have a massive problem.

How we fixed it

Simply put, I wrote the will_paginate plugin. Instead of just having ‘Next’ and ‘Previous’, it also displays a window of page numbers that allows bots the ability to navigate hundreds more pages within one click. Now we’re getting somewhere.

Eureka!

As the traffic began to come back in a serious way, well beyond where it was before the dip, the notion of SEO became quite clear to me. Given a site with tons of great content, instead of building more features, you should be trying to leverage what you already have.

This also makes a lot of sense from a financial standpoint. How long would it take your team to build a new feature for 10k more uniques a day, how long would it take them to add meta tags and a sitemap for 10k more uniques a day. There’s no comparison.

The Checklist

SEO isn’t rocket science (unless you’re trying to push the envelope), you just have to be aware of a few guidelines.

Page-specific
  • Unique title tag
  • Unique meta description (Yahoo is the only one using meta keywords anymore)
  • One h1 tag (the text used in your h2-h4 tags is still very important, though)
Site-wide
  • Allow only quality outbound links (eg. rel="nofollow" on all user-generated links)
  • Block duplicate content from being indexed with robots.txt
  • Structure your CSS so the content can go first
  • Consistent navigation that allows bots to get to any page on your site within 2 or 3 clicks

Piece of cake. This discussion can go far deeper than these points, but if you do the stuff above, you’re 90% of the way there. The last 10% is the most time-consuming, so I leave that to you to decide if it’s worth your time.

I know a lot of people think of SEO as a dirty word, and that it conjures up negatives connotations of link-baiting and spam, but that really couldn’t be further from the truth. I’m just going to start calling it FT…Free Traffic.

Speaking of Which

Err Free does more than just Rails, we’d be happy to apply what we’ve learned about SEO to your site. Who doesn’t love Free Traffic?

JRuby Reaches 1.0

Posted about 7 years back at zerosum dirt(nap) - Home

JRuby 1.0 has been officially released. This is huge news. Complete compatibility with Ruby 1.8 is pretty much a done deal. Rails apps run. You can deploy your Rails apps in Glassfish. I mean, how cool is that?

You should check out team member Charles Nutter’s blog for more details. In a nutshell, the future looks like tighter integration with Java as an application platform, and a major focus on optimizing performance. Also a lot of interest in Ruby 1.9/2.0. If you attended Railsconf this year, you most likely got pretty excited about JRuby, and for good reason.

If you’ve never tried it out before, Atlantic Dominion Solutions has put together a great tutorial on getting started with JRuby on OS X. Or, even easier, go get yourself a copy of NetBeans 6.0 M9, which comes bundled with JRuby as the default interpreter for in-IDE development.

Episode 42: with_options

Posted about 7 years back at Railscasts

Several methods in rails take a hash of options as the last argument. If you are passing the same options to several methods, you can remove this duplication by using with_options. Learn all about it in this episode.

NHRuby Meeting Tuesday 06.12

Posted about 7 years back at zerosum dirt(nap) - Home

Usually the NHRuby meets are scheduled for the third Tuesday of the month, but we had to change it up this time due to some schedule conflicts.

I’ll be speaking about integrating a Flash widget with Rails-based RESTful web services and also giving a brief overview of how to contribute to Rails and other open source projects (I guess you could say Josh Susser’s talk at Railsconf inspired me). Scott will be doing an overview of the tabnav plugin and… something else, too.

More information and directions can be found at the NHRuby Wiki. Also, many thanks to our new host/sponsor for the meetings, RMC Research.

Franken-php-rails-apache-stein

Posted about 7 years back at work.rowanhick.com

Every wondered about the possibility of running a rails app, and a php (or anything else for that matter) site together, on one virtual host, on apache. But why? the hordes screamed ... DHH keels over ... the PHP community says ahh just put it in CakePHP... and to you it feels just plain *wrong*. However, there is always that odd need, every now and then, that you have to do it. So here's how. It's really simple, assuming you're using mongrel cluster and setup a balancer in Apache for it, like so in one of the conf files... <Proxy balancer://mybalancer.rowanhick.com> BalancerMember http://localhost:2000 BalancerMember http://localhost:2001 BalancerMember http://localhost:2002 </Proxy> Then on the paths that you want to mash up with your vhost, it's as simple as this... For specific file mapping to an action: ProxyPass /some_path/some_file balancer://mybalancer.rowanhick.com/mycontroller/myaction ProxyPassReverse /some_path/some_file balancer://mybalancer.rowanhick.com/mycontroller/myaction ProxyPreserveHost On For one path mapping to a controller: ProxyPass /some_path/ balancer://mybalancer.rowanhick.com/mycontroller/ ProxyPassReverse /some_path/ balancer://mybalancer.rowanhick.com/mycontroller/ ProxyPreserveHost On Restart and you're away. (even if it feels dirty)

Our Contribution to Advanced Rails Recipes

Posted about 7 years back at Revolution On Rails

We submitted a few recipes, answering the call for sharing what is happing in the rails community, and a half a dozen of them were accepted as entries to the upcoming book. If you enjoyed reading the blog, you would soon have a chance to see some of it on paper.

Our Contribution to Advanced Rails Recipes

Posted about 7 years back at Revolution On Rails

We submitted a few recipes, answering the call for sharing what is happing in the rails community, and a half a dozen of them were accepted as entries to the upcoming book. If you enjoyed reading the blog, you would soon have a chance to see some of it on paper.

RailsConf Recap: Named Callbacks

Posted about 7 years back at The Rails Way - all

Another topic we touched briefly on at RailsConf was the idea of named callbacks.

Consider this snippet (also from Brian Cooke’s expense tracking application):

1
2
3
4
5
6
7
8
class Expense < ActiveRecord::Base
  protected
    def before_create
      if self.created_at == Time.now.to_date.to_time
        self.created_at = Time.now
      end
    end
end

One thing to keep in mind here is that when a new Expense record is created, the created_at column is used to track when the expense originally occurred, not when the record was created. As a special case, if the timestamp is 00:00 of the current day, then it is assumed to actually be the current time.

Now, looking at that code, it’s definitely not immediately obvious what it is trying to do. In fact, it took me a few minutes of steady concentration (and cross-referencing other parts of the project) to understand it. The fact that it uses a generic “before_create” callback makes it hard to know the purpose of the method, and the use of “Time.now.to_date.to_time” (though effective) is pretty intention-obscuring.

Here’s a clearer, more self-documenting approach, using a named callback:

1
2
3
4
5
6
7
8
9
10
class Expense < ActiveRecord::Base
  before_create :make_created_now_if_created_today

  protected
    def make_created_now_if_created_today
      if self.created_at == Time.now.beginning_of_day
        self.created_at = Time.now
      end
    end
end

The named callback helps make it clearer what the purpose of the method is (though in this case, an additional comment would not be amiss). Also, ActiveSupport comes to the rescue, allowing us to convert the convoluted “Time.now.to_date.to_time” into the more self-documenting “Time.now.beginning_of_day”. (Alternatively, you might prefer “Time.now.midnight”, though I find “beginning_of_day” to be clearer, since it reveals the intention better.)

Always look for ways to make your code document itself. Ruby is one of the most readable programming languages I’ve ever used, and it’s a pity to not take advantage of that readability as often as you can.

Rails Consultant wanted

Posted about 7 years back at RailsExpress.blog

I’m looking for an experienced Rails consultant to complement the team working for Autoscout24 on the International Sales Synergy Project which I am managing.

Requirements:

  • brightness
  • ability to get things done
  • willingness to dive into an existing code base, fix stuff and extend existing functionality
  • good social skills
  • ability to speak English (German is a plus)

If you’re interested, send me your credentials.

Update: the open position has been filled. Thanks for sending in your emails.

Episode 41: Conditional Validations

Posted about 7 years back at Railscasts

By default, validations will take place every time the model is saved. Sometimes you only want a validation to happen when certain conditions are met. See how to do that in this episode.

LOLCODE

Posted about 7 years back at zerosum dirt(nap) - Home

[3:44pm] strager: BUKKIT is the array type. [3:44pm] zapnap: O RLY?