Learn10 Must Have Ruby Gems

Treehouse
writes on March 3, 2011

One of the most beautiful things about Ruby development is the ease of adding functionality through packaged libraries called gems. With the power of Bundler, you can quickly add and manage gems in few lines of code. With the recent release of Rails 3.0.4 I’d like to share the 10 must-have gems which allow me to focus on what’s unique to my app.

1. Devise (Authentication)

Just about every public-facing Rails app needs some type of authentication scheme. Authentication is often confused and interchanged with authorization, although the 2 terms have very distinct meanings.

In a nut shell, authentication determines if users are who they say they are, commonly through a username and password combination. Authorization determines which actions an authenticated user can perform. Devise is a solution to the former problem. I recently switched from AuthLogic in favor of Devise’s advanced engine for providing models, controllers and views. AuthLogic leaves it up to the developer to create the views and controllers. However, views are generally the first thing you should override, but thankfully Devise makes that easy as well.

Devise is a very active gem, and in conjunction with omniauth, has made it incredibly simple to setup Facebook and Twitter login buttons in less than a dozen lines of code. At first, the engine seemed too overbearing for me, but once I understood how easy it is to override and extend Devise, I made the switch.

https://github.com/plataformatec/devise

2. CanCan (Authorization)

CanCan is an incredibly easy way to define and access user permissions. All apps tend to have some sort of authorization scheme, but it’s generally setup adhoc, and leads to very messy view code. If you have dozens of if object.user == current_user statements littered among your views and controllers, it’s time to take a look at CanCan.

If you aren’t authorizing the current user to modify a provided object in update actions, a user could send a post request to update another user’s project, for example. With CanCan, your app’s authorization scheme is defined centrally in an Ability model. By using the can? method, you can check to see if the current user is authorized to perform an action to handle conditional view rendering.

CanCan also makes it dead simple to authorize controller actions and handle authorization exceptions.

https://github.com/ryanb/cancan

3. Haml (Better Views)

HAML transforms your views with sexy, minimalist syntax that makes Ruby jealous. HAML’s indent based syntax eliminates end tags, and provides a more concise way of defining HTML and interpreting embedded ruby code.

I made the switch about 2 years ago and cringe every time I have to work with “real” HTML. Even mediocre HTML ninjas will be comfortable with HAML in a few days. HAML takes:

<h2><%= @user.name %></h2>
<div class="about">About Me: <%= @user.about %></div>

and makes it great:

h2= @user.name
.about About Me: #{@user.about}

http://haml-lang.com/

4. Compass (Better CSS)

Compass is a framework that does to CSS what HAML does to HTML. In conjunction with SASS, a CSS replacement, Compass provides native frameworks such as Blueprint. By leveraging SASS mixins, you can finally ditch non-semantic classes such as “div-8” for blueprint columns.

The other major benefit of SASS is variables, most commonly used for color definitions. Tear up that sticky note with hex colors and start using color variables. Better yet, create an entire color palette using SASS’s lighten and darken functions.

http://compass-style.org/

5. Will Paginate (Pagination)

Will Paginate is by far the reigning champ when it comes to pagination. Originally ported from PHP, will_paginate can get you up and running with pagination in a few lines of code. There are practically no contenders, and rightfully so since I can’t think of too many ways to make this better. OK, Ajax pagination would be really awesome.

https://github.com/mislav/will_paginate/wiki

6. Paperclip (File Attachments)

If you’ve ever written code to handle file uploads and image processing, you surely cringe every time a client says “I’d like the user to have his or her own photo”–that is until you’ve worked with a gem as easy to use as this one.

Paperclip makes it trivial to restrict content types, define storage locations and access a model’s associated attachment. If you’re working with images, you can define styles with corresponding resolutions for use-cases such as thumbnails and profile sizes.

Even if you are using Heroku with its read-only file system, paperclip works beautifully with outside storage mechanism such as Amazon S3.

https://github.com/thoughtbot/paperclip

7. Meta Where (ActiveRecord Query Extensions)

This is a less popular gem that I ran into when I got sick of passing in complex SQL strings into ARel where methods, and then finding the inconsistencies between MySQL (development) SQLite (test) and Postgres (Heroku).

The default behavior of chaining ARel where methods is to “and” the conditions. Meta_where lets you define complex boolean logic on conditions, even among sub tables.

http://metautonomo.us/projects/metawhere/

8. DelayedJob (Background Job Scheduling)

One of the easiest ways to speed up some Rails apps is to push more complex processing into background jobs. The most notorious offenders are image processing and emailing.

Instead of having the user wait on the browser while an email sends or an image is processed, (both items that can wait) offload these tasks to a later time and let the user continue.

This gem automatically handles scheduling and provides an easy way to run all scheduled jobs. With Heroku, running the jobs is as easy as pushing up the workers a notch.

https://github.com/tobi/delayed_job

9. Has Scope (Collection Filtering)

This gem does to filtering what will_paginate did to pagination. Most rails apps dealing with item collections that need to be filtered by an attribute such as location, category or tag, will benefit from this. By leveraging existing model scopes, you can keep your controllers lean without conditional statements for each possible filter query.

Simply use the has_scope method with the scope name in the controller, and pass the model into apply_scopes to take advantage instant filtering.

https://github.com/plataformatec/has_scope

10. Rack SSL Enforcer (HTTPS Redirection)

With all the hype a few months ago over Firesheep, even basic, non credit-card accepting, apps that use passwords need for SSL. Sure, there’s no requirement that you secure your app, and even if a user’s account was compromised it may cause little harm.

However, most people reuse the same password for everything, and I wouldn’t like to have on my conscience that a user’s online banking password was stolen because they accessed my app without SSL at Starbucks.

Rack SSL Enforcer is a dead simple gem that uses Rack to redirect non https requests to the https equivalent. The default configuration forces HTTPS on every request, but can easily be overridden to only required it on some URL and URL patterns.

You may be familiar with the ssl_requirement gem originally released by DHH. This isn’t compatible with Rails 3, although there is a fork bartt-ssl_requirement that is which I’ve used in previous projects. I never really liked the idea of HTTPS redirection happening after the request gets to the controller. The rack solution is much simpler, especially for securing an entire app.

https://github.com/tobmatth/rack-ssl-enforcer

40 Responses to “10 Must Have Ruby Gems”

  1. “HAML take” … “and makes it great” and makes it like weird garbage.
    HTML (which erb basically is) is ideal. Very straightforward and understandable, unlike hieroglyphic haml/slim. Never would recommend to use it.

  2. 最も美しいスーパーコピー,超格安ブランド時計コピー激安通販専門店!ブランド品に憧れて、予算オーバーし、諦めなければならないことがありましたか。スーパーコピー品が一番いいチョイスだ思います。少しのお金でも、世界中一番人気を持つブランド品、貴族しか買えない贅沢品がゲットできます。素敵な外観と実用性に優れたブランド コピー バッグ,,スーパーコピー財布,スーパーコピー 代引き,エルメス コピー,ルイヴィトン 財布、ルイヴィトン バッグ、ベルトなどの逸品はお客様に提供します。誰でもブランド品を手に入れられるのは弊社の経営理念です。当店の最大目標はお客様が安心してお買い物が出来き、お客様の頼りになるお店を目指す事ことです。お洒落な貴方はきっとこのチャンスを見逃しなくよ!
    ブランドコピー時計大人気を海外激安通販専門店ロレックス、ブルガリ、フランク ミュラー、シャネル、カルティエ、オメガ、IWC、ルイヴィトン、オーデマ ピゲ、ブライトリング、グッチ、エルメス、パネライ、パテックフィリップ偽物(コピー商品)のブランドの腕時計の販売、通販。 http://www.newkakaku.net/co.htm

  3. ブランドN級品ブランドコピー 代引き,スーパーコピー時計,ブランドN級品,楽天コピーブランド,,偽物ブラン日本最大級の最高のスーパーコピーブランド財布激安代引き販売店,スーパーコピー時計の激安老舗.!国内No.1時計コピー工房,アフターサービスも自ら製造したスーパーコピー時計なので、技術力でお客様に安心のサポー トをご提供させて頂きます。スーパーコピー 代引きN品をご 購入の方は、こちらへ.弊社は正規品と同等品質のコピー品を低価で お客様に提供します!すべての商品は品質2年無料保証です。
    ロレックスコピー販売、代引きロレックス時計コピー通販、全て新品、高い品質、激安、送料無料。ロレックス時計コピーなど世界中有名なブランドレプリカを格安で通販しております。N級品スーパーコピーブランドは 業界で最高な品質に挑戦しますロレックコピー,ロレックコピー代引き,ロレック激安,ロレックス偽物 ,最高級ロレックコピーロレックス時計コピー,フェラーリコピー時計,パネライコピー時計,パテックフィリップコピー時計,ヴァシュロン.コンスタンタンコピー時計,エルメスコピー時計,カルティエコピー時計ルイヴィトンコピー、 ロレックスコピー、シャネルコピー、グッチコピー、エルメスコピー、 ボッテガヴェネタコピー、 バーバリーコピー、ミュウミュウコピー、トリーバーチコピー、バレンシアガコピー、ディオールコピー、ブルガリコピー、ブラダコピー、 ドルチェ&ガッバーナコピー、オメガコピー、フランク ミュラーコピー、gagaコピー。古驰拷贝, 靴/サンダル,サングラスコピー欧米O級品オーダー服各種のブランドの服、靴、、財布、腕時計の複製品をかばん一番ブランドliveブランドコピー服,ブランドバッグコピー,ブランド時計,ブランド靴シリーズ欧米O級品コーナーラッピング用品 http://www.gowatchs.com/brand-261.html

  4. Please add my gem to this list https://github.com/igorkasyanchuk/rails_db to explore DB and run SQL in browser

  5. Nice one 🙂
    I have made a list of gems which every one should use on their development environment.
    http://www.codebeerstartups.com/2013/04/must-have-gems-for-development-machine-in-ruby-on-rails/

  6. andrewhavens on March 28, 2013 at 1:43 pm said:

    In my opinion Kaminari is a much better pagination gem (https://github.com/amatsuda/kaminari).

  7. Anonymous on April 16, 2011 at 3:25 pm said:

    Yeah.. great list.. But I would suggest carrierwave over paperclip

  8. Anonymous on March 10, 2011 at 7:03 pm said:

    Surprising blog it is a you have a great knowledge about the the gems . And you had make a different category for that . I hope you had done a great research behind this .

    wholesale gemstones

  9. I’d take resque over delayed-job

  10. Err… “10 Must Have *Rails* Gems”

    And I would disagree on a few accounts such as Ham and Compass –too many layers of abstraction.

  11. It’s worth noting that delayed_job on rubygems.org points to the Collective Idea fork, https://github.com/collectiveidea/delayed_job.

  12. Anonymous on March 4, 2011 at 1:38 pm said:

    I’m more of a Carrierwave fan than Devise, and I am in two minds with ssl-enforcer, a lot of what it does can be done in the routes file, but that said, it does reduce some of the clutter in your routes file.

    I really like some of the ideas behind meta where and hope to see some of it included in AR in 3.1.

    And I would also have to side with some of the comments about resque being better than delayed-job.

  13. agree on everything, but not on haml+compass/sass… i prefer pure html + lesscss… everyone knows html, it’s not the same for haml.. it’s an extra language to learn… and designers aren’t so happy usually (same for sass)

  14. what about nifty-generators? rest-client is good.

  15. what about nifty-generators? rest-client is good.

  16. C00lryguy on March 4, 2011 at 6:22 am said:

    What the fuck? There’s more to ruby than Rails. I can think of PLENTY of gems that are a must-have and have NOTHING to do with web application development.

  17. Danielnavin Desuza on March 4, 2011 at 4:12 am said:

    Nice and useful post.. Loved it.
    http://leanmusclex.org

  18. Other than HAML and Compass, this is a pretty good list.

    • Wow, you don’t like HAML/SASS/Compass? Everyone I meet loves it!

      • That’s strange, because less than half of ruby developers use haml, and shrinking it seems:

        http://survey.hamptoncatlin.com/survey/stats

        Personally I hate it.

        • Anonymous on March 4, 2011 at 2:26 pm said:

          Haml is truly awesome. It allows you to write views faster than you can with HTML, and that’s a fact and worth using for that reason alone. The views created by haml are a LOT cleaner and smaller than html can be.

          Because it uses indentation for structure its a lot easier to restructure your haml file by just changing the indentation level of blocks of text, you just don’t have that kind of easy manipulation with html. I use the awesome haml plugin for vim.

          I chose Rails as my web dev environment so I could write apps quicker, easier and with less hassle, and those are the exact same reasons I use haml.

        • techieBrandon on April 21, 2013 at 11:08 pm said:

          I love HAML, took a few weeks to really get into the groove with it but after getting a deep understanding it is much cleaner than HTML in general.

          In complicated html it can quickly loose its indentation formatting and closing tags can easily be lost, resulting in lost time trying to locate the correct tag to close. Obviously this is a indication that the HTML likely needs broken into further fragments but none-the-less it happens and can easily be avoided with the use of HAML.

          • Nicholas Johnson on July 3, 2013 at 12:34 pm said:

            I wrote HTML for 15 years, then I discovered HAML, now I won’t write anything else. It’s like the difference between JSON and XML. No one should be forced to hand code XML.

      • I like compass, it brings value, but I don’t feel like that for haml! If I work with designer I can explain why to use compass, but what can I say for haml? Just looks better? The only thing I don’t like with scss, but can’t think of the way to eliminate it, is that indentation stuff.

  19. Thanks for the MetaWhere mention! 🙂

  20. Kaminari is very cleanly implemented alternative to will_paginate for pagination in Rails 3 projects:
    source: https://github.com/amatsuda/kaminari
    railscast: http://railscasts.com/episodes/254-pagination-with-kaminari

    CarrierWave is a rapidly maturing upload manager that is more flexible and fully featured than Paperclip:
    source: https://github.com/jnicklas/carrierwave
    railscast: http://railscasts.com/episodes/253-carrierwave-file-uploads

    There’s lots of active development out there. Ruby Toolbox is one way to sort through it all:
    http://www.ruby-toolbox.com/

  21. Carrierwave and resque are way better than paperclip and delayed job 🙂

Leave a Reply

You must be logged in to post a comment.

Want to learn more about Ruby?

Ruby is a dynamic, open source programming language with a focus on simplicity and productivity. Learn how to build a website using Ruby and the Rails framework.

Learn more