运行环境:netbeans IDE 6.1
今天下午本来打算对院务系统做个模拟联系。按照自己习惯,先在控制器controler中生成一个新的控制器,之后在view中建立控制器的方法xxx.rhtml。之后在rhtml中写上了<h1>院务系统</h1>。运行了一下,在浏览器中显示的是乱码。太郁闷了。
在网上找了很多方法,试了一下午,都没有试出来。
包括以下的方法
把xxx.rhtml文件用记事本打开,然后另存为编码为utf-8的xxx,rhtml。再运行一下,果然可以输出中文,但是我再用netbeans打开,rhtml中的中文却是乱码的。更加郁闷。这样多不方便以后编译。
还有一种方法,网上很多人在用的,不过对于我没有用。
- 修改ApplicationController 如下:
class ApplicationController < ActionController::Base
before_filter :configure_charsets
def configure_charsets
# @headers["Content-Type"] = "text/html; charset=utf-8"
@response.headers["Content-Type"] = "text/html; charset=utf-8"
# Set connection charset. MySQL 4.0 doesn??t support this so it
#will throw an error, MySQL 4.1 needs this
suppress(ActiveRecord::StatementInvalid) do
ActiveRecord::Base.connection.execute 'SET NAMES utf8'
end
end
end
- 请在rhtml中,或者适当的layout中,加上:
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
- 在config/database.yml修改如下:development:
adapter: mysql
database: depot_development
encoding: utf8 *注意空格*
username: root
password:
host: localhost
保存后,重启ROR,启动app,再次ruby script/generate scaffold Product Admin应该就行了。数据库里的表应该都是utf8_general_ci,rhtml也应该都是utf-8编码。/app/views/layouts/admin.rhtml中加上<meta>标签。
-
试了一个下午,绝望死了。最后是师兄发现问题所在。(他太牛了,看了几下,就找出问题所在)
-
原来我在项目的属性中的编码,没有设置是utf-8的。汗......