Rails force input to encode in windows-1250

时间:2016-04-25 09:35:07

标签: ruby-on-rails utf-8 character-encoding

I'm making an app on existing database in firebird with charset windows-1250. And im getting a lot of troubles because of this encoding, but i cant change it.

My biggest error now is when im making form for input object called notification like this:

 form do |f|
  panel Iconv.conv('windows-1250', 'utf-8', 'Opis zgłoszenia:'), 
    :class=> 'panel_dodaj_new' do
      f.input :opis_zgloszenia, label:false
    end
 end


It try to save text in utf-8, but i want it to save in windows-1250.
How to force :opis_zgloszenia to save in different charset than utf-8?

2 个答案:

答案 0 :(得分:0)

我找到了改变控制器编码的方法 在我的创建方法中,我添加:

@notification.opis_zgloszenia = 
Iconv.conv('windows-1250', 'utf-8', @notification.opis_zgloszenia)

我还需要更改我的更新方法:

if @notification.update(notification_params)
    @notification.opis_zgloszenia = Iconv.conv('windows-1250', 'utf-8', @notification.opis_zgloszenia)
    if @notification.save
      redirect_to request.url
    end
  end

现在一切正常。

答案 1 :(得分:0)

我认为更合适的解决方案是为整个数据库配置默认编码。这样,我希望Rails透明地转换数据库(Windows 1250)和Rails代码/网页(UTF-8)之间的编码。您应该可以使用encoding配置中的database.yml设置进​​行设置。

所以类似下面的内容应该对你有用:

development:
  adapter: fb
  database: db/development.fdb
  username: SYSDBA
  password: masterkey
  host: localhost
  encoding: windows-1250
  create: true

有关详细信息,请参阅Firebird adapter documentation