I've recently been working on converting my 'rosters' rails 1.x app to rails version 2, and there are plenty of pitfalls that i spent lots of time googling around trying to find the solutions. So, for just as much need for my own future reference as anyone else's, here are all the specific traps *I* ran into, and their solutions, in the one place. If you're interested in the source code to the whole app, you can see the rails 1.x version here: http://rosters.rubyforge.org/ The new version is running here, and if enough people request it, i'll release the code.

Basics

Firstly, I created a new rails 2 skeleton app, using 'rails myappname' from the command line. Then i copied the contents of all my app and public folders over from the old app to the new one. Finally i went through the db and config directories, modifying the new files manually, rather than simply overwriting them with the old ones. This way you'll get to see how the configuration files (especially routes!) have changed, and won't be overwriting them with legacy code (it feels strange to refer to rails 1 as 'legacy'!)

What? Rails is embracing security now?

Forms now have a special 'key' that needs to be present for POSTs, to prevent cross-site-request-forgery (try saying that 10 times fast!). The gist of it is that you can't just have code like this in your views any more: <form name="f" action="/account/login" method="post" onsubmit="wait();"> ... </form> Now what you want is more like this: <% form_tag("/account/login", :name=>'f', :onsubmit=>"wait();") { %> ... <% } %> The above code takes care of the new CSRF-proof keys and all that. As you can see, my example is a bit more complex than usual, with a form name and an onsubmit javascript callback, but hey, its real code! For a simpler, bare-bones form, Rails 2 style, here you go: <% form_tag :action=> "new" do %> ... <% end %>

Where on earth did 'find_first' go?

Back in controller-land, i hit problems with all my find_first's that i like to use. This kind of thing simply wont work any more: u = find_first(["login = ? AND password = ?", login, sha1(pass)]) You need something more like this nowadays: u = find :first, :conditions=>["login = ? AND password = ?", login, sha1(pass)] It appears that all the find_* functions (find_first,find_all,etc) have all been rolled into the one-function-to-rule-them-all 'find' function. I guess it is neater.

No more @session or @request

This one's simple. Simply replace these kind of things: @session @request With this: session request One big 'find-in-files' (Ctrl-Shift-F in Notepad++) will sort you out with this one.

Redirect

Another simple one, but its likely you'll have one of these in each of your updating actions: redirect_to_url '/blah/foo' ...becomes... redirect_to '/yada/yada'

RIP Pagination

Apparently pagination wasn't quite good enough to meet muster for Rails 2, so its been scrapped totally. There's no simple replacement for this one, but hopefully you're only really using it in your admin scaffolds, so what i did was a simple pagination-dectomy in my controllers: @user_pages, @users = paginate :users, :per_page => 30, :order=>'login' Became: @users = User.find :all, :order=>'login' Its pretty simple, and you'll need to remove all references to pagination in your views. If you really *really* need pagination, you'll need something more complex, but there isn't anything out-of-the-box for you. For more details: http://wiki.rubyonrails.org/rails/pages/HowtoPagination

End Results

Well, now the site's up and running, you're all welcome to have a look: http://rosters.morphexchange.com/

Thanks for reading! And if you want to get in touch, I'd love to hear from you: chris.hulbert at gmail.

Chris Hulbert

(Comp Sci, Hons - UTS)

iOS Developer (Freelancer / Contractor) in Australia.

I have worked at places such as Google, Cochlear, Assembly Payments, News Corp, Fox Sports, NineMSN, FetchTV, Coles, Woolworths, Trust Bank, and Westpac, among others. If you're looking for help developing an iOS app, drop me a line!

Get in touch:
[email protected]
github.com/chrishulbert
linkedin
my resume



 Subscribe via RSS