If you use gettext in your web application, you’ll sooner or later have to face the problem of handling links that should be embedded into a translated text. What can easily be done in a normal, non-localized template with ERb, can become a nightmare if used extensively in localized templates. However you can prevent yourself (and your translators) a lot of nightmares by using a small helper method.
Read more »Articles tagged with: rails - Page 3
TMail create_forward fixed
A minor bug in TMail (which is used in Rails’ ActionMailer) got fixed finally.
About 2 years ago, I reported bug #6015 to the Ruby on Rails bug tracker. TMail’s create_forward method always returned the first body part of a newly created forwarded mail instead of the forwarded mail itself. Therefore the create_forward method was practically useless. At that time, the ActionMailer and/or TMail developers didn’t seem to be very active. Well… nobody cared about this for about two years.
After I heard that TMail got a new maintainer, I gave it another try and submitted bug #15445 to the newly created TMail bug tracker. Mikel (who is the new TMail maintainer) applied and tested a fix, so the upcoming TMail version will come without this bug. Thanks a lot, Mikel.
Let’s hope this fix will make it into Rails 2.0 so that I can dump one more workaround in my application soon.
Paginating special queries with HasFinder and will_paginate
Trying to paginate special queries gave me a headache for quite some time. As summarized in one of my previous blog posts “Paginating special queries”, custom finder methods do not work without problems if you use the will_paginate plugin.
There are several plugins that make life easier when you need custom finder methods to do special queries in your models. Widely known are scope_out and scoped_proxy. Both of them are great plugins with different features, but unfortunately neither of them work out of the box with pagination.
Some days ago, Nick of Pivotal Blabs brought back to my mind, that there’s another plugin called HasFinder. HasFinder basically works like scope_out and scoped_proxy and combines advantages of both plugins. Besides the basic idea to define different scopes for an ActiveRecord model, you can also access methods (finder) in nested scopes that exactly behave like ActiveRecord associations do. Additionally, HasFinder works out of the box with will_paginate.
Head over to Nick’s HasFinder article to find out more about it. HasFinder is available as a gem or can be installed as a plugin from SVN. Great plugin, Nick.
Update 2008-04-02: HasFinder functionality will be included in Rails 2.1, called named_scope
Flash not accessible in tests (TypeError: can't convert String into Integer)
This morning I faced a strange problem when trying to write RSpec tests for a user login action. Trying to setting the flash before doing a request, resulted in an unexpected Exception:
Read more »TypeError in ‘SessionController logging in with correct credentials should redirect to the url_after_login url if given’
can’t convert String into Integer
…/vendor/plugins/rspec_on_rails/lib/spec/rails/dsl/behaviour/functional.rb:80:in `flash’
./spec/controllers/session_controller_spec.rb:52:
./script/spec:4:
Vanishing records on creating multiple models in one action
Did you ever want to create multiple models in one action? E.g. if your user model has many email addresses, create a user record and his first email address when he signs up… Nick of Pivotal Blabs wrote a nice article about it – but it almost drove me to despair this morning when I was trying to break it down to my user and email addresses model.
Read more »Symbolize attribute values in ActiveRecord
ActiveRecord does not natively suppport column types of ENUM or SET. If you want an attribute to act like an ENUM, you’ll most probably use a string and restrict it to certain values using validates_inclusion_of. However, once you’ve got used to Ruby, you’d probably prefer to have symbols as values for these attributes. Here’s a small and easy-to-use snippet that can be used to symbolize values of any ActiveRecord attribute.
Getting an old-style revisions.log with Capistrano 2.0
Capistrano 2.0 does not write a revisions.log anymore when deploying your application. However, I liked the revisions.log, because it gives you a clear history of which revision you deployed to your servers at which time. Therefore I wrote a little Capistrano recipe which updates an old-style revisions.log whenever you deploy or rollback your appliaction.
Read more »Scope_out feature: default_scope
I suppose, you know the great scope_out plugin for rails. If not, go check it out, since it’s really great to define DRY scoped associations. Basically, you can use scope_out to define the scoped associations within the model the scope is applied to. However, what I’m missing is a feature like a default scope that can be applied to the model and scopes the default finder.
Read more »Using ruby-gettext with Edge Rails
The more I read about the various new features coming up in Rails 2.0, the more I couldn’t resist… and finally, a few days ago, I switched over to Edge Rails with my current project.
Switching to Edge Rails was easily done, however after fireing up /script/server and doing the first request, I was presented with a 500 Internal Server Error.
DISPATCHER FAILSAFE RESPONSE (has cgi) Sun Jul 29 12:43:56 +0200 2007
Status: 500 Internal Server Error
You have a nil object when you didn’t expect it!
You might have expected an instance of ActiveRecord::Base.
The error occurred while evaluating nil.[]
/usr/lib64/ruby/1.8/cgi.rb:1165:in `[]’
/usr/lib64/ruby/gems/1.8/gems/gettext-1.10.0/lib/gettext/locale_cgi.rb:26:in `system’
/usr/lib64/ruby/gems/1.8/gems/gettext-1.10.0/lib/gettext/locale.rb:88:in `system’
/usr/lib64/ruby/gems/1.8/gems/gettext-1.10.0/lib/gettext/locale.rb:96:in `default’
After some time of debugging, it turned out to be ruby-gettext, which I use for I18N, that causes a NoMethodError on each request during init-gettext.
Read more »Paginating special queries
The will_paginate plugin for Rails is one of the best pagination plugins I know of and I like the ease of using it. Instead of using Model.find, simply use Model.paginate(:page => x) and you’ll get back a collection that behaves like an array and can be displayed in views like any other collection. Additionally the returned collection has some new methods (like page_count and total_pages), which are useful to display pagination links. Installing the will_paginate plugin adds the pagination class method to all models; it even works with automatic finders: e.g. you can use Model.paginate_by_attr, which will paginate queries for Model.find_by_attr.
However, pagination can become tricky if you want to paginate collections returned by special queries…
Read more »