Problem: pagination

Jeffrey Hardy,
Dec 11, 2007

On page 172 of the book, we talk about Pagination. Pagination is a good thing. As a rule, don’t return more results from the database than you need to for a given purpose.

In the sample application, we use Rails’ built-in Paginator, but with the caveat that it might be slated for future removal from the framework. Alas, that day has come. In Rails 2.0, pagination is no more. Why? Well, as I mentioned in the book, a lot of folks feel that pagination is too domain-specific to be part of the framework.

You can install the classic_pagination plugin, which is the extracted code from Rails, rolled into a plugin. This is what we used in the book. Installation is the same as for any plugin:

./script/plugin install svn://errtheblog.com/svn/plugins/classic_pagination

That’ll get you up and running with the examples in the book. This has bitten at least one reader already. Read up on the thread on the book’s mailing list

Enter: will_paginate

Pagination was removed from Rails because it was deemed outside the scope of the framework. This is good. Removing a feature like this is usually a catalyst for the emergence of better alternatives. Out of the handful of pagination plugins currently circulating, my favorite is will_paginate:

http://errtheblog.com/post/4791 http://require.errtheblog.com/plugins/browser/will_paginate/README?format=txt

This is from will_paginate’s README:

# app/controllers/posts_controller.rb

@posts = Post.paginate :page => params[:page]

# app/views/posts/index.html.erb

<p>Handful of posts coming up</p>
<%= render :partial => 'post', :collection => @posts %>

<p>Now let's render us some pagination!</p>
<%= will_paginate @posts %>

Note that will_paginate’s interface is different than that of classic pagination, so it’s not a drop in replacement. Still, if you read the examples in the README, you should be able to figure it out in no time!

On sessions, cookies, and debugging in Rails

Jeffrey Hardy,
Nov 28, 2007

To borrow a move from Ruby Inside, here are a few Rails tidbits that don’t need separate posts, but that you’re likely to find quite useful when learning Rails.

Everything you need to know about sessions and cookies

This article is long. But it’s the best writeup on sessions and cookies as they pertain to the Rails framework I’ve yet read.

http://www.quarkruby.com/2007/10/21/sessions-and-cookies-in-ruby-on-rails

Debugging Rails applications with ruby-debug

The days of the breakpointer are long behind us. The new tool on the block is ruby-debug. Thankfully, Patrick Lenz has written the most complete guide to ruby-debug I’ve seen to date.

Debug your Rails app with ruby-debug

Ruby Naming Conventions

Jeffrey Hardy,
Oct 19, 2007

From the Softies on Rails comes a quick summary of idiomatic Ruby naming conventions. To a seasoned Rubyist, these are common knowledge, but I’ve seen many a beginner carrying their baggage from other languages over to Ruby.

Ruby 101: Naming Conventions

In the book, I wrote the following sidebar that covers the basics (from page 330):

Ruby Style

Style is important when programming. Ruby programmers tend to be picky about style, and generally adhere to a few specific guidelines:

  • Indentation size is two spaces
  • Spaces are preferred to tabs
  • Variables should be lowercase and underscored: some_variable, not someVariable or somevariable
  • Method definitions should include parentheses and no unnecessary spaces: MyClass.my_method(my_arg), not my_method( my_arg ) or my_method my_arg

Whatever your personal style, the most important thing is that you remain consistent. There’s nothing worse than looking at code that switches between tabs and spaces, or mixed and lowercase variable names.

Becoming an Expert in Rails is Hard

Jeffrey Hardy,
Sep 30, 2007

Jay Fields has an insightful article on the fact that, while it’s relatively easy to get started with Rails, experts aren’t born overnight.

It is easy to learn Rails and quickly become productive. However, the dirty little secret is that it’s very hard to be come an expert at utilizing Rails.

Reading Beginning Rails will not make you an expert. It will introduce to you the Rails philosophy, and take you through the basics, but in order to build a successful web application using Rails, you need to become proficient in a lot of technologies. Web applications have a lot of moving parts. In addition to learning Ruby, you’ll need to learn SQL, JavaScript, Ajax, HTML, and YAML, and a collection of others. You’ll also need to learn how to test, and how to debug. If your web application is to become popular, you’ll need to learn how to scale it in a production environment and how to optimize your database server.

There is no shortcut to success here. Mastering web development takes time and effort. Reading a book like Beginning Rails is really only the first of many investments in your knowledge portfolio, something that requires constant investment over time.

Read the full article at Jay’s blog here.

Raaum's Rails Reader

Jeffrey Hardy,
Sep 28, 2007

Raaum’s Rails Reader is a great resource for beginners and experienced Rails developers alike. It serves a both a manual for Rails and a field guide to help you navigate the wealth of other Rails documentation scattered throughout the web.

http://rails.raaum.org

Some choice cuts include:

Thanks Raaum!

Rails Console Secrets

Jeffrey Hardy,
Sep 19, 2007

This is an oldish link, but it’s a good one, especially for those folks who aren’t yet Rails console ninjas.

Secrets of the Rails Console Ninjas by Amy Hoy.

For the unenlightened, Ruby comes with a an interactive interpreter called irb (for Interactive Ruby). Rails harnesses the power of irb to give you an interactive console for your application (hereafter referred to as simply “the console”). The advantage of the console is that it enjoys the special privilege of being fully integrated with your project’s environment. This means it has access and knowledge of your models (and subsequently, your database). You can execute any arbitrary Ruby code inside the console and do anything you might otherwise do inside your Ruby programs: set variables, evaluate conditions, and inspect objects. The only essential difference between an interactive session and a regular old Ruby program is that console will echo the return value of everything it executes. This saves you from having to explicitly print the results of an evaluation. Just run the code, and you’ll see the results.

We use the console pretty heavily in the book, so for the most part, you learn by doing. But there are definitely some tricks here that we didn’t mention. So, if you’re wondering about things like:

  • Specifying console environment (./script/console production)
  • Getting out of a block of code (ctrl + c)
  • Accessing the last return value (_)
  • Reloading the console (reload!)

Then you should check out Amy’s article. There’s also a collection on Stupid console tricks!

For further reading, check out Chris Wanstrath’s classic article irb Mix Tape, which is full of other amazing goodies that will make your day.

Dynamic Scaffolding and Edge

Jeffrey Hardy,
Sep 5, 2007

We talk about dynamic scaffolding in Rails starting on page 54, Up and Running with Scaffolding, but we need to mention something: the dynamic scaffolding we’re using has been moved out of Rails core and into a plugin.

Now, don’t panic. This won’t affect you if you’re using the latest stable version of Rails (1.2.3 at the time of this writing), but if you’re using edge Rails, it will.

Fortunately, installing the scaffolding plugin couldn’t be any easier:

./script/plugin install scaffolding

The plugin script tries to look for plugins in certain locations, including the Rails svn URL (try ./script/plugin sources to see the complete list of sources). If for some reason it fails to find scaffolding by name, you can always use the full path:

http://svn.rubyonrails.org/rails/plugins/scaffolding

If you’re following along with the book using edge Rails, you’ll need to install this for the examples to work!

(I’m actually a fan of the dynamic scaffolding. I find it useful to be able to tweak my schema and model and play with it before generating boilerplate templates. Actually, I tend to use dynamic scaffolding more that the generated variety. Just my personal preference, though.)

Cheatsheets

Jeffrey Hardy,
Sep 5, 2007

I was going through some bookmarks today and I came across a few cheatsheets that I thought would be of interest to readers.

First, there’s Jens-Christian Fischer’s PDF cheatsheet for Rails. It doesn’t cover version 1.2, but most of the information is still relevant. When you need to look something up quickly, you can just search the PDF. Handy.

Another Rails cheatsheet I really like is from ilovejackdaniels.com. It’s a good quick reference that includes the default directory structure, predefined variables, reserved words, and more. Definitely one to check out.

If you’re looking for a Ruby-only cheatsheet, try RubyCheat on for size. And while technically not a cheatsheet, I also find ZenSpider’s Ruby QuickRef pretty useful when the PickAxe is not handy.

Choice Gem: cheat

Speaking of cheats, one of my favorite Ruby Gems as of late is Chris Wanstrath’s cheat.

As with all Ruby Gems, it’s easy to install:

$ sudo gem install cheat
Bulk updating Gem source index for: http://gems.rubyforge.org
Successfully installed cheat-1.2.1

Basic usage is straightforward: you run the cheat program and give it the name of the cheatsheet as an argument. So, if you forget all your MySql commands, you can just run cheat mysql.

Here’s another example, and one I use all the time—strftime (output truncated, of course):

$ cheat strftime
strftime:
    %a - The abbreviated weekday name (``Sun'')
    %A - The  full  weekday  name (``Sunday'')
    %b - The abbreviated month name (``Jan'')
    %B - The  full  month  name (``January'')
    ...

There are a bunch of Rails-related sheets which you can see by typing cheat rails. Here’s the full list:

  • radrails_rhtml
  • radrails_ruby
  • rails_console
  • rails_edge
  • rails_request_object
  • rails_reserved_words
  • rails_tasks
  • rails_to_s
  • rails_vim
  • test_spec_rails
  • textmate_rails

So, if you’re trying to check out a working copy of edge rails, but forget the path to the Rails subversion repository, you can quickly type cheat rails_edge to be reminded.

To see all available cheatsheets in a big long list, do this:

$ cheat sheets
All Cheat Sheets:
  arts
  as3_formulas
  ascii
  assertions
  assert_select
  association_methods
  awk
  balloon
  bash
  ...

Finally, to see more of what you can do with cheat, including examples and useful command line flags, use cheat’s own cheatsheet:

$ cheat cheat
cheat:
  Hey, welcome to cheat (http://cheat.errtheblog.com).
  Thanks for trying it out. 
  ...

Happy cheating!

Starting Rails: what Kalid Azad wishes he'd known 1

Jeffrey Hardy,
Aug 7, 2007

I just came across Starting Ruby on Rails: What I Wish I Knew in which Kalid Azid summarizes what to expect from Rails and its relatives, including Rake, IRB, and YAML. Also mentioned are various Ruby-isms and Rails-isms that often elude the novice developer.

Ruby on Rails is an elegant, compact and fun way to build web applications. Unfortunately, many gotchas await the new programmer. Now that I have a few rails projects under my belt, here’s my shot at sparing you the suffering I experienced when first getting started.

This is the exact question I asked myself while writing Beginning Rails: what do you wish you’d known when you first started out. So, if you’re reading the book and are wishing for a concise summary of some of the various gotchas, do yourself a favor and read Kalid’s article.