Jump to content

The ultimate community for Ruby on Rails developers.


Photo

rails3 and ruby 1.9: Encoding ISO-8859-1

encoding rails3 ruby1.9

  • Please log in to reply
2 replies to this topic

#1 João e o pé de feijão

João e o pé de feijão

    Passenger

  • Members
  • 5 posts

Posted 24 January 2014 - 12:15 PM

Hi

 

I found a bug on RAILS3!

 

I have a application configured for ISO-8859-1

 

config/application.rb:

    config.encoding = "ISO-8859-1"
    config.action_controller.default_charset = 'ISO-8859-1'
    config.action_dispatch.default_charset = 'ISO-8859-1'

AND my database.yml:

development:
  adapter: ibm_db
  database: DBDES1
  username: abc
  password: abc
  schema: glb
  host: localhost
  port: 60002
  pool: 5
  encoding: iso-8859-1

THE PROBLEM:

when I call the object, the value is UTF-8

irb(main):001:0*a=GlbProfissao.find 80
  GlbProfissao Load (1.2ms)  SELECT glb.glb_profissao.* FROM glb.glb_profissao WHERE glb.glb_profissao.cdg_profissao = ? FETCH FIRST 1 ROWS ONLY
=> #<GlbProfissao cdg_profissao: 80, dsc_profissao_masculino: "Capit\u00E3o", dsc_profissao_feminino: "Capit\u00E3", cdg_usuario_alt: 205, dta_alteracao: 
irb(main):002:0* a.dsc_profissao_masculino
=> "Capit\u00E3o"


#2 Ohm

Ohm

    Guard

  • Members
  • 184 posts
  • LocationCopenhagen

Posted 24 January 2014 - 02:19 PM

Are you sure that your IBM db is configured for ISO-8859-1?

 

It's not enough to put it in database.yml, the actual database must also be configured properly.

 

Also note that UTF-8 is (and rightly should be) the standard to use, and in Rails 4 (with Ruby 2) everything is UTF-8.


Blog: http://ohm.sh | Twitter: madsohm


#3 João e o pé de feijão

João e o pé de feijão

    Passenger

  • Members
  • 5 posts

Posted 24 January 2014 - 05:50 PM

My DataBase is ISO-8859-1

 

I think I unique that working with ISO-8859-1 in Rails3, IBMDB, ruby 1.9, ...

 

THE SOLUTION:

 

So if someone have the problem do it; you'll rewrite a IBM_adapter method

module ActiveRecord
    class IBM_DataServer
      # FIXME
      # Alteração dos valores para o encode ISO-8859-1
      # ibm_db/lib/active_record/connection_adapters/ibm_db_adapter.rb => line 1928
      def select(stmt)
        results = []
        begin
          while single_hash = IBM_DB.fetch_assoc(stmt)
            #results <<  single_hash
            results << single_hash.inject({}){|ac, v| ac[v.first]=(v.last.encode('iso-8859-1') rescue v.last);ac}
          end
        rescue StandardError => fetch_error # Handle driver fetch errors
          error_msg = IBM_DB.getErrormsg(stmt, IBM_DB::DB_STMT )
          if error_msg && !error_msg.empty?
            raise StatementInvalid,"Failed to retrieve data: #{error_msg}"
          else
            error_msg = "An unexpected error occurred during data retrieval"
            error_msg = error_msg + ": #{fetch_error.message}" if !fetch_error.message.empty?
            raise error_msg
          end
        end
        return results
      end
    end#IBM_DataServer
  end #ConnectionAdapters
end







Also tagged with one or more of these keywords: encoding, rails3, ruby1.9

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users