Ruby 对外文字符的支持

通常Rails应用程序都习惯使用英文作为源码的编码格式。

由于项目要求,在数据库表名以及字段名需要使用外文字符。

经过调查。这种需求可以实现。使用方法如下:

1.在使用到外文字符的源码文件头上加上“# encoding: utf-8”,注意必须在文件第一行第一列开始

2.文件名使用英文

3.类名使用英文

4.migration文件采用如下方式:

# encoding: utf-8
class AddChineseTalbe < ActiveRecord::Migration
  def change
    create_table :你好 do |t|
      t.string :你的
      t.string :method

      t.timestamps
    end
  end
end

5.model文件的写法

# encoding: utf-8

class CN < ActiveRecord::Base
  self.table_name = :你好
end

6.controller中的写法

# encoding: utf-8
class IndexController < ApplicationController
  def index
    c=CN.new 你的:"我的"
    c.save

    @c=CN.find 1

  end
end

 

posted on 2013-10-25 08:33  leonworld2011  阅读(141)  评论(0)    收藏  举报

导航