Ruby Forum Rails I18n > Gettext error_messages_for problem

Posted by Eugene (Guest)
on 06.02.2007 15:14
Hello,

Can anyone help with my problem? When trying to add validation to Model
like "validates_uniqueness_of" and then put <%= error_messages_for
'article' %> into view, I get error.

can't convert Array into String

Extracted source (around line #1):

1: <%= error_messages_for 'article' %>


Please advice.

Thank you.
Posted by Florian Hufsky (Guest)
on 30.04.2007 17:46
ruby-gettext error_messages_for wasn't up-to-date with latest rails 
changes.

i've fixed it in my version, maybe this helps you.

you need to edit gettext's rails.rb and in module L10n remove 
render_error_messages_for and replace the following two functions 
(L10n.error_messages_for and ActiveRecordHelper::error_messags_for) by


<pre>        def error_messages_for(params, klass)
          options = params.last.is_a?(Hash) ? params.pop.symbolize_keys 
: {}
          objects = params.collect {|object_name| 
klass.instance_variable_get("@#{object_name}") }.compact
          count   = objects.inject(0) {|sum, object| sum + 
object.errors.count }

          unless count.zero?
            html = {}
            [:id, :class].each do |key|
              if options.include?(key)
                value = options[key]
                html[key] = value unless value.blank?
              else
                html[key] = 'errorExplanation'
              end
            end

            record = 
ActiveRecord::Base.human_attribute_table_name_for_error((options[:object_name] 
|| params.first).to_s)

            message_title = @error_message_title
            message_explanation = @error_message_explanation

            header_message = n_(message_title, count) % {:num => count, 
:record => record}
            error_messages = objects.map {|object| 
object.errors.full_messages.map {|msg| klass.content_tag(:li, msg) } }

            klass.content_tag(:div,
              klass.content_tag(options[:header_tag] || :h2, 
header_message) <<
                klass.content_tag(:p, n_(message_explanation, count) % 
{:num => count}) <<
                klass.content_tag(:ul, error_messages),
              html
            )
          end
        end
      end
      def error_messages_for(*params)
        L10n.error_messages_for(params, self)
      end
    end</pre>
Posted by Stephan Oehlert (steevo)
on 04.10.2007 11:51
Can someone confirm that this solution works? I have run into this 
problem as well and can't get it to work yet. I use Rails 1.2.3, Ruby 
1.8.6. Except for this problem, Gettext works well with rails.

The problem is:

TypeError: can't convert Array into String
/opt/local/lib/ruby/gems/1.8/gems/gettext-1.10.1/lib/gettext/active_record.rb:243:in 
`=~' 
/opt/local/lib/ruby/gems/1.8/gems/gettext-1.10.1/lib/gettext/active_record.rb:243:in 
`localize_error_message' 
/opt/local/lib/ruby/gems/1.8/gems/gettext-1.10.1/lib/gettext/active_record.rb:242:in 
`each' 
/opt/local/lib/ruby/gems/1.8/gems/gettext-1.10.1/lib/gettext/active_record.rb:242:in 
`localize_error_message' 
/opt/local/lib/ruby/gems/1.8/gems/gettext-1.10.1/lib/gettext/active_record.rb:273:in 
`localize_error_messages'
[...]


Thanks a lot