Tag Archives: rubyonrails

37signals also saw the benefits of AWS

37signals moved TaDaList to run on pretty much the same things I’m using: EC2, EBS, Elastic IPs, Apache, Passenger. They also started from the same Ubuntu Intrepid images. 🙂

Joshua posted more info on their setup in the comments section of the blog post:

Joshua Sierles 28 Nov 08

Matt,

Our custom image is based on the Ubuntu Intrepid images from Alestic. We install useful EC2 gems and base packages, bundle, then provision each instance by role. Working with EC2 is so easy, we don?t see much value in using a third-party provider for our scale.

Yaroslav,

EBS and Elastic IPs were the primary motivation for moving to EC2 . We use EBS extensively: for MySQL data, logs and local repository mirrors. Performance so far is excellent. Snapshotting volumes is a breeze and makes setting up MySQL slaves and staging environments really easy.

[From Ta-da List on Rails 2.2, Passenger And EC2 – (37signals)]

Rails plugin/helper to provide “click to copy text field” (to clipboard)

You often see sites with text fields of share links in various different formats, like Skitch provides links to share the images you skitch, they look like this:

Skitch.com > andrewng > Energy Saver
Uploaded with plasq‘s Skitch!

You often click the text box or a copy button next to it and the content is copied to your clipboard so you can paste it into a tweet, a blog or forum post and what not.

I needed something like this for one of my projects, it is pretty easy to do, and there are things you can use to do this. However there isn’t a super simple way to do this in Rails, maybe it’s so simple that nobody bothered to publish a plugin. Well, just for kicks I’ve created a plugin that lets you do exactly this by using a cilick_to_copy_text_field_tag helper in your views. This plugin requires jQuery or jRails, neither of which is part of standard Rails, but they are pretty easy to add to any Rails project.

The plugin is hosted here: http://github.com/ayn/click_to_copy_text_field/tree/master, the README pretty much tells all you need to know to use it.

Using Phusion Passenger in Facebook apps development

Ray pointed me to a RailsCast on how to use Passenger for local development, it literally took me 2 minutes to setup. If you work on multiple Ruby on Rails applications, you should definitely do it.

There is however one problem on this approach, since the default passenger.pref config relies on virtual host, you can’t really setup SSH port forward to forward something to one of the virtual hosts, and this is important as this is how most of us develop Facebook applications locally. To get Passenger and Rails Facebook apps to work, you have to:

  • open up httpd.conf ask Apache to listen to another port:
Listen 81
  • add another named virtual host, here I’m adding port *.81:
<IfModule passenger_module>
  NameVirtualHost *:80
  NameVirtualHost *:81
  Include /private/etc/apache2/passenger_pane_vhosts/*.conf
</IfModule>
  • In the configuration for that Facebook app, change it to respond to all traffic on the new port (81 in this case):
<VirtualHost *:81>
  ServerName app_name.local
  DocumentRoot "/Users/ayn/work/app_name/public"
  RailsEnv development
  RailsAllowModRewrite off
  <directory "/Users/ayn/work/app_name/public">
    Order allow,deny
    Allow from all
  </directory>
</VirtualHost>

Now you can setup your tunnel to forward to localhost (or 127.0.0.1 if you use SSHKeychain) port 81 and it should work. Add more ports if you work on multiple Facebook apps at the same time.

autocomplete with multiple related form fields (in rails)

I?m an advisor and consultant for a social network site where you go upload images and tag the stuff you own and the stuff you want. One of the thing we wanted to do in the stuff-tagging form was to have related autocompletion fields. In this case, we want an autocompletion of brand names, and then based on the brand name input, we narrow down the autocompletion for models to only the models for that brand. This should be a pretty commonly done, but the auto_complete plugin in Rails has no easy way to do this.

The obvious approach was to use Rails? autocompletion plugin to do the first field, and then use Scriptaculous directly for the second field, my first approach looked like this:

<%= text_field_with_auto_complete :product, :brand %> 

<input id="product_model" name="product[name]" size="30" type="text" />
<div class="auto_complete" id="product_model_auto_complete"></div>

<script type="text/javascript">
  new Ajax.Autocompleter(
    'product_model',
    'product_model_auto_complete',
    '/products/auto_complete_for_product_model',
    { parameter: 'brand=' + $F('product_brand') }
  );
</script>

But this didn?t work because the parameters are populated on instantiation of the Ajax.Autocompleter, so dynamic form content are not properly sent to the controller. Took some googling and playing around to figure this out, but the solution is to append the additional parameter by overriding the callback.

<%= text_field_with_auto_complete :product, :brand %> 

<input id="product_model" name="product[name]" size="30" type="text" />
<div class="auto_complete" id="product_model_auto_complete"></div>

<script type="text/javascript">
  new Ajax.Autocompleter(
    'product_model',
    'product_model_auto_complete',
    '/products/auto_complete_for_product_model',
    { callback: function(e, qs) {
        return qs + '&brand=' + $F('product_brand');
      }
    }
  );
</script>

Just for fun, I?m including the ajax endpoint to show how I generate the results in the desired unordered list format using the auto_complete_result helper. You should always specify a limit in these type of queries.

def auto_complete_for_product_model
  @results = Product.find_by_sql(
  [ %Q{select p.name from brands b, products p
      where lower(b.name) = ? 
      and p.brand_id=b.id 
      and lower(p.name) like ? limit 10}, 
    "#{params[:brand].downcase}", 
    "#{params[:product][:name].downcase}%" ]) if params[:brand] && params[:product][:name]    
    render :inline => "<%= auto_complete_result @results, 'name' %>"
end

BTW, this site is gonna make an appearance at the Techcrunch 50 demo pit, I should be there, check my Tweets, stop by and say hi if you happen to be around.

Dealistic launched!

I’m happy to tell you that we have finally launched Dealistic! It is a side project Ray and I spent most of our free time in the past 3 months on. We created Dealistic because both of us were frustrated on the existing deals sites on the interweb — they never deliver deals on the things we really want, so we created Dealistic to help us narrow them down to only the deals we care about.

The site is pretty simple, Sherry made an awesome walkthrough on what the site is about.


Dealistic Walkthrough from Andrew Ng on Vimeo.

Check it out, give us feedback and comments at our GetSatisfaction page, hope you find it useful as well.