Edge Rails and gettext: undefined method file_exists? (NoMethodError)

Posted by Andreas on Tuesday, February 12, 2008 at 17:08 (CET)

After updating to the latest edge Rails via SVN today, my local development webserver suddenly started to respond with a 500 application error for every request. The mongrel console revealed:

NoMethodError (undefined method `file_exists?’ for #<ActionView::Base:0x2aaaac3eee10>):
/usr/lib64/ruby/gems/1.8/gems/gettext-1.90.0/lib/gettext/rails.rb:280:in `render_file’
/usr/lib64/ruby/gems/1.8/gems/gettext-1.90.0/lib/gettext/rails.rb:278:in `each’
/usr/lib64/ruby/gems/1.8/gems/gettext-1.90.0/lib/gettext/rails.rb:278:in `render_file’
/vendor/rails/actionpack/lib/action_controller/base.rb:1107:in `render_for_file’
/vendor/rails/actionpack/lib/action_controller/base.rb:841:in `render_with_no_layout’
:
:

Digging deeper, I found out that the method ActionView::Base#file_exists? has been moved to a different place in Rails’ Changeset 8683. Fortunately, fixing it is easy.

As a workaround until either rails or gettext is fixed, you can simply drop the following lines somewhere into your rails application (e.g. I added it to config/initializers/gettext.rb, which I use to pull in gettext support):

1 module ActionView
2   class Base
3     delegate :file_exists?, :to => :finder unless respond_to?(:file_exists?)
4   end
5 end

This snippet adds the method file_exists? to ActionView::Base if it doesn’t exist and delegates calls to it to the finder (which is the place where this method has been moved to in newer rails versions).

Update: Issue reported as ruby-gettext bug #17990.

Update 2008-06-18: In a recent commit, the ActionView::Base#file_exists? method was added again, so the above workaround isn’t necessary anymore for Edge Rails and probably Rails 2.1.

Update 2008-08-07: Looks like something was changed again before Rails 2.1 was released. With Rails 2.1, this workaround is still necessary to use gettext.

7 comments

Gravatar
John D. Rowell wrote 12 days later:

Thanks Andreas, I just updated my pistonized rails edge and ran into the same problem, even with the latest gettext gem. Didn’t try gettext svn though (and don’t plan to :P). Cheers!

Gravatar
Antono Vasiljev wrote about 1 month later:

Thank you for your snippet! You saved my time :)

Gravatar
Ramon Cahenzli wrote 4 months later:

Thanks from me, also, especially for the workaround. gettext is already fixed now, I see, so hurray for free software :)

Gravatar
Raul Riera wrote 5 months later:

Hi, this fix doesnt seem to be really “fixed” for Gettext and Rails 2.1 I still get the error

NoMethodFound for init_gettext if I dont have that workaround in my initializers. Any thoughts?

Gravatar
Andreas wrote 5 months later:

Yes indeed it looks like something was changed again before they released Rails 2.1, so this workaround is still necessary to use gettext with Rails 2.1. Hopefully the upcoming i18n interface of the next Rails version will make it easier to keep translation plugins like gettext working.

Gravatar
Raul Riera wrote 5 months later:

Gettext has a new version to fix this issue, thanks for your help

Gravatar
Hallison Batista wrote 6 months later:

Thanks … this code snippet saved my project.

Leave a comment

(required)

(required; will not be published)

(optional)

(required; Textile formatting allowed)