msgbartop
泰州软件开发 Tony Lee 心生软件
msgbarbottom

20070417 Ruby&Rails中文问题

要确定你的mysql数据库用的是什么字符集,可以通过命令来看

mysql> show create database <你的数据库名>

可以用下面的命令建立一个gbk字符集的数据库

mysql> create database xxxx default character set gbk

要确定你连接的的字串encode,比如在config/database.yml中

development:
adapter: mysql
database: collector
encoding: gbk
username: root
password:
host: localhost

在config/enviroment.rb 最后面增加一行

$KCODE=”gbk”

修改MySQL的配置文件C:\Program Files\MySQL\MySQL Server 5.0\my.ini

default-character-set=utf8(共两处,这里也可以在安装数据库的时候就选择好)

修改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″>

20070417 关于h()这个方法

关于h()这个方法
h其实是html escape
Rails has a helper method,
going to want to use it when substituting values into HTML pages.
Email: <%= h(“Ann & Bill <[email protected]>”)%>
In this example, the h() method prevents the special characters in the
e-mail address from garbling the browser display—they’ll be escaped as
Email: Ann & Bill <[email protected]>—the
HTML entities. The browser sees
special characters are displayed appropriately
.
显示特殊的符号, 避免符号被”处理” ,更进一步的来看, 从安全角度, h 也辅助防止被”攻击”, 比如有恶意之徒, 提交的内容里有html的字符
ruby的核心库文档
Module ERB::Util
In: lib/erb.rb

A utility module for conversion routines, often handy in HTML generation.
Methods
h html_escape u url_encode
Public Instance methods

h(s)

Alias for html_escape
html_escape(s)

A utility method for escaping HTML tag characters in s.

require “erb”
include ERB::Util

puts html_escape(“is a > 0 & a < 10?”)

Generates

is a &gt; 0 &amp; a &lt; 10?

[Source]

# File lib/erb.rb, line 780
780: def html_escape(s)
781: s.to_s.gsub(/&/, “&amp;”).gsub(/\”/, “&quot;”).gsub(/>/, “&gt;”).gsub(/</, “&lt;”)
782: end

u(s)

Alias for url_encode
url_encode(s)

A utility method for encoding the String s as a URL.

require “erb”
include ERB::Util

puts url_encode(“Programming Ruby: The Pragmatic Programmer’s Guide”)

Generates

Programming Ruby :  The Pragmatic Programmer’s Guide

20070417 Ruby&Rails的IDE

学习一门新语言,真的太不容易了,首先难过的一关,就是写代码的工具——IDE。

关于Ruby和Rails的东西,在google和百度上面搜索下,竟然出来可数的页数,从头到尾翻看了一遍,把所有有关IDE的文章看了下,然后看见一个就下载一个来装,什么scite、RDE、FreeRIDE、Notepad++、RadRails、Eclips+RDT……所有能用的,我都装了,但是……没有我喜欢的。对ROR的支持,都只是代码着色、测试、Debug,最多再集成下数据库、WEB Server,到此为止了,也许作为一个IDE,做到这些就够了,但是,难道就不可能有一个IDE,像JBuild写Java、VS写C#那样舒服顺手的么?即便是动态语言,一些基本的东西还是可以有代码提示的啊。郁闷。

于是乎,绕了一大圈,最后还是回到Ruby默认安装的SCiTe了……还研究了一会它的配置文件。郁闷、郁闷……

偶尔发现了Dreamweaver的ROR插件,还算顺手,搞到最后,还是用我那看腻了的DW8。还是郁闷。

于是乎,非常期待DW9……中文版赶快出(不出也没有多少关系,因为学ROR,我看的英文资料也不少了,已经锻炼到看英文书不头疼的地步了)最主要的是——不要钱的赶快出!

残念……