Tag Archives: rails

If you see weirdness when using jquery.iframe-transport.js and/or remotipart…

This took a while to figure out. Remotipart overrides the render function in Rails to return the response in a textarea that jquery.iframe-transport.js expects to see, if you’re working with JSON (and who isn’t?), make sure you don’t have any type of JSON pretty-formatter browser add-on/plugin/extension installed.

That is all. And AJAX file upload still is a pita to do.

Don’t run MySQL 5.5 on OSX

So while setting up a Rails2 development environment with RVM for one of my clients on my new Mac, I came across the infamous uninitialized constant MysqlCompat::MysqlRes error. Typically this is because the mysql gem isn’t compiled with the right archflags, but of course that wasn’t the case. After messing with it for a bit, I found out that the MySQL server I was running was 5.5.10, and 5.1 is supposedly more stable. So I downgraded it and everything worked.

credit: this post helped a bit.

Pullfolio private beta! Here’s your invite!

If you’re a pro/semi-pro photographer and love Flickr, I think you will love Pullfolio. Pullfolio helps you create professional portfolio sites using photos in your Flickr account by tag(s) or photosets. For example, you can setup your photography site with these portfolios:

  • Wedding – it pulls all photos that are tagged pullfolio and wedding (or whatever tag(s) you specified when you configure the portfolio) from your Flickr account
  • Portraits – it pulls all photos from your portraits photoset on Flickr

You get the idea. You can also enter your blog URL, bio, and contact info, and they will be included on your site. You can also choose from a few themes. If you upgrade to the Pro subscription for just $15 a month, you will be able to point your domain to the site, add your analytics code for tracking, and if you want to get your hands dirty, you have the ability to completely customize your site. We plan to partner with designers to provide more themes, and if you need a designer, we can provide referrals to ones who understand how our themes work.

When you upload new photos to Flickr, you will just have to tag them or add them to a photoset and your portfolio website will have the new photos automatically.

You can check out my photography site to get an idea of how it works. I still have to setup my own portfolios properly though.

If this sounds like it’s something you want, I am giving out 100 invites, here’s a direct link to signup for our private beta:

Each account also comes with 5 additional invites upon signup.

A bit of background: as a heavy Flickr user, I’ve always wanted a photography site that pulls photos from my Flickr account. This way I can upload my photos to Flickr with the right tags or photoset, and it will automatically show up at my photography site. After not having a proper photography site for more than 3 years, there still isn’t a service out there that does what I want. It’s also quite shocking that I was able to get gigs by networking and my Flickr photostream (or “collections” rather) without a website. When Ray and I were brainstorming ideas of what to build, I selfishly suggested this idea. We got a prototype working within 2 weeks, however, to turn the idea into a real product that supports many users, themes, subscriptions, custom domains, while using behavior-driven development with 100% test coverage, was a significant effort.

Feel free to post any feedback at our UserVoice feedback forum. You can follow Pullfolio on Twitter to get updates on new features.

RailsRumble ’09

I participated in last weekend’s RailsRumble, my first time “rumbling”, it was a pretty awesome experience. Ray‘s team at Intridea had an opening and he asked if I wanted to join last week, I was like, why not. I’ve been doing paired coding with Ray for years now and I know we can easily pull it off in 48 hours. We also had Doug March, an awesome designer who helped with the design and layout of the app. Check out our app: MovieTwitvia

I don’t really have anything profound to say about the whole experience, but a few random things:

  • Linode was awesome, I requested to install and boot to Ubuntu jaunty and I was able to ssh in pretty much instantly
  • It took a while to get Github access, but that didn’t stop us to git init and start hacking
  • I used Sprinkle to setup our Passenger stack, with a few small exceptions it worked flawlessly, we were able to `cap deploy` within half hour of the start of the competition. I thought about trying deprec but at the time of server setup I didn’t have Rails app setup yet so I went with Sprinkle, which I’ve used quite extensively at Inigral
  • since our app uses Twitter authentication, I used mbleigh’s twitter app template, I had issues with one of his plugins in the past so I was uneasy using it, but I have to say it worked great for us
  • We used Pivotal Tracker and planned out the features before the app. They fall nicely into Cucumber features after we started coding. Ray and I wrote Cucumber features and steps for all our model codes. We didn’t follow through with integration testing, mocking all that API stuff would’ve been a bit of a pain, and our app is pretty simple
  • (pickler didn’t work for me, I think it doesn’t work with the version of Cucumber I used)
  • Ray and I coded til 7am my time Saturday night (or Sunday morning), at that time our app was pretty much done, only major thing we had to do was to put in the reply box so users can reply to the question on our site directly. We were pretty pumped up and thought about working for a few more hours, but decided that it was probably better to get a few hours of sleep, so we can hack with better focus and more energy til deadline. It was a good decision, I got up at around 10:30 and hacked out that reply box pretty quickly
  • I actually had to go to tomales bay state park for a BBQ so I had to leave at 2pm California time, Ray and Doug took care of the final touches and the verification steps, it was nice to have finished all features way ahead of time and we were able to fix bugs and try out the sites with a few friends before the end of the competition
  • I found one bug in the code this afternoon, it’s very minor and probably won’t matter unless we get more than a certain number of replies for a given question 🙂

That’s basically all I have to say right now, if we get through to the public voting round, please vote for us!

RESTful in-place edit in Rails and jRails/jQuery

I’ve been using jRails in my recent Rails projects, the original Rails in-place editing plugin uses script.aculo.us, there is a jRails version of it, but neither of them is RESTful – they both create extra actions to update the in-place edit fields.

I found janv’s rest_in_place plugin and it uses the default update action to update the field, so no routes modifications are necessary. I had some problems with the plugin at first but after a pull-request correspondence the plugin now works well. Here are the highlights on how to use it, keep in mind that I use HAML.

The plugin’s init.rb doesn’t load anything for you, so you have to go into your application layout and include the js file:

    = javascript_include_tag 'jquery.rest_in_place.js'

If you have CSRF protection on, this plugin also requires you to set a javascript var. If you have jRails it automatically append the token in ajax requests, but then you would have to modify the plugin a bit to get it to work.

:javascript
  rails_authenticity_token = '#{form_authenticity_token}'

In your controller’s show action, handle the javascript response:

  def show
    respond_to do |format|
      format.html # show.html.erb
      format.js   { render :json => @model }
    end
  end

then you can render the helper in the views:

- div_for @model do
  %p
    = label_tag "Name"
    %br
    %span.rest_in_place{ :attribute =>'name' }
      =h @model.name
  %p
    = label_tag "Location"
    %br
    %span.rest_in_place{ :attribute => 'location' }
      =h @model.location

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.