2009年1月23日 星期五

Migrations in Rails 2.1

Migrations in Rails 2.1
有跑過的migration, 會紀錄在schema_migrations 裡
強化了一些change column的功能

Non Active Record Model

Non Active Record Model

Going Back

Helpers Outside Views

Helpers outside views

2009年1月21日 星期三

JSON

2009年1月20日 星期二

javascript tips

改變 iframe 的 source

http://twpug.net/modules/newbb/viewtopic.php?post_id=7602
http://www.javaworld.com.tw/jute/post/view?bid=34&id=161191&sty=3

javascript in browser tips

1. window 是最上層的object, document 是其中的一個屬性
2. window 指的是視窗, frame, iframe, document 代表HTML 文件
3. example: parent.frames[0].documnet.forms[0].elements[3].options[2].text
4. javascript 不能跨網域使用, 但是指的並不是該javascript 的來源source, 而是嵌入, 用該javascript的地方

2009年1月19日 星期一

cookies 設定

cookies api
# :value - The cookie‘s value or list of values (as an array).
# :path - The path for which this cookie applies. Defaults to the root of the application.
# :domain - The domain for which this cookie applies.
# :expires - The time at which this cookie expires, as a Time object.
# :secure - Whether this cookie is a only transmitted to HTTPS servers. Default is false.
# :http_only - Whether this cookie is accessible via scripting or only HTTP. Defaults to false.

cookies 的一些觀念
1.要在這個domain 跟 path之下的才能拿, 例如如果把domain設成
ilake.demo.com, demo.demo.com 這樣彼此是不能拿的, 可是設成 demo.com 便可以

2. cookies 誰都可以寫, 寫就是寫了, https 寫的 http 也可以拿, 但是可以設成只有 https 才可以拿, :secure 就是此選項, 而http_only 便是javascript 不能拿

3.不能把cookie domain 設成你現在server domain 以外的 domain

4.navigator.cookieEnabled 可以檢查cookie 功能是否開啟

5.cookie 不能包含分號 逗號 空白, 所以有時妳會需要用encodeURIComponent, 讀取時就要使用相對應的decodeURIComponent

6.刪除cookie, 就是把他直接設過去的時間即可

7.cookie 只能有4K, cookie 在妳request 其符合規則網頁時(same domain, path etc...)
就算妳沒用它, 它也會上傳到server


另外 rails cookies 裡有 subdomain_fu 在localhost 這種 domain 之下, 有點問題, 把他弄成demo.com 這樣正常的domain, 便可以正常拿到該domain 下的cookies

2009年1月16日 星期五

javascript在網頁頁面加載時的執行順序

javascript在網頁頁面加載時的執行順序

javascript在頁面的執行順序

1、頁面上的javascript代碼是HTML文檔的一部分,所以javascript在頁面裝載時執行的順序就是其引入標記<[script /]>的出現順序, <[script /]>標記裡面的或者通過src引入的外部JS,都是按照其語句出現的順序執行,而且執行過程是文檔裝載的一部分。


3、變量的調用,必須是前面已經聲明,否則獲取的變量值是undefined。

5、document.write()會把輸出寫入到腳本文檔所在的位置,瀏覽器解析完documemt.write()所在文檔內容後,繼續解析document.write()輸出的內容,然後在繼續解析HTML文檔。

test.js的內容是:

var tmpStr = 1;
alert(tmpStr);

在Firefox和Opera中的彈出值的順序是:1、2、我是1、3

在IE中彈出值的順序是:2、1、3,同時瀏覽器報錯:tmpStr未定義

原因可能是IE在document.write時,並未等待加載SRC中的javascript代碼完畢後,才執行下一行,所以導致2先彈出,並且執行到 document.write(』document.write("我是" + tmpStr)』)調用tmpStr時,tmpStr並未定義,從而報錯。

解決這個問題,可以利用HTML解析是解析完一個HTML標籤,再執行下一個的原理,把代碼拆分來實現:

這樣IE下和其他瀏覽器輸出值的順序都是一直的了:1、2、我是1、3。

三、如何改變javascript在頁面的執行順序

1、利用onload

輸出值順序是 2、1。

需要注意的是,如果存在多個winodws.onload的話,只有最有一個生效,解決這個辦法是:

window.onload = function(){f();f1();f2();.....}

ruby gzip, stringio

string io api
gzip api
我們想要壓縮一下sting, 可以用

iframe 之間的互動

在一層又一層的iframe 裡, 我們可以利用 top 屬性直接跳到最上層的視窗操縱其他我們想操縱的 iframe

example
[ 內部 iframe]

[ 外部 iframe]

當然為了各個iframe之間的溝通, 或許會有 same-origin policy 的問題
所以各個iframe 都需有將iframe 設成相同的 code, 例如很多使用subdomain 的 domain name
便可以如此使用

2009年1月15日 星期四

textarea 點選自動複製, copy

parent document

javascript 在要parent 時的這個parent 必須是在相同的 protocol, 跟 domain 下

2009年1月14日 星期三

get,submit

2009年1月13日 星期二

activerecord, query cache

query cache

CSRF (Cross-Site Request Forgery) & InvalidAuthenticityToken & protect_from_forgery

protect_from_forger api

http://www.g0574.com/thread-221-1-1.html

CSRF (Cross-Site Request Forgery) & InvalidAuthenticityToken & protect_from_forgery

protect_from_forger api

http://www.g0574.com/thread-221-1-1.html

2009年1月12日 星期一

some tips, add_to_base, save_with_validation, verify

some tips, add_to_base, save_with_validation, verify

2009年1月10日 星期六

Some cgi memo

cgi api

CGI 這個 class 提供了一些好用的 method, 一些 url, html 的 encode ,decode

Some cgi memo

cgi api

CGI 這個 class 提供了一些好用的 method, 一些 url, html 的 encode ,decode

javascript escape html

在rails, 要利用javscript 生成 html 內容要escape 一下, 這時就是用 escape_javascript

javascript escape html

在rails, 要利用javscript 生成 html 內容要escape 一下, 這時就是用 escape_javascript

ruby & javascript url encode

api

而ruby 相對應的 methhod 就是 escape 啦

ruby & javascript url encode

api

而ruby 相對應的 methhod 就是 escape 啦

rails 辨識brower

可以用這樣來辨識brower

rails 辨識brower

可以用這樣來辨識brower

2009年1月9日 星期五

Some good tips

http://errtheblog.com/posts/42-rails-rubyisms-advent







Some good tips

http://errtheblog.com/posts/42-rails-rubyisms-advent







字串 String 一些好用的 function, scan, split, match, gsub, sub, grep

Scan


Split

Match and Scan 感受一下啥時要用啥吧

gsub and sub 就是 replace 只是一個全部, 一個只有第一個


字串 String 一些好用的 function, scan, split, match, gsub, sub, grep

Scan


Split

Match and Scan 感受一下啥時要用啥吧

gsub and sub 就是 replace 只是一個全部, 一個只有第一個


字串 String 一些好用的 function, scan, split, match, gsub, sub

Scan


Split

Match and Scan 感受一下啥時要用啥吧

gsub and sub 就是 replace 只是一個全部, 一個只有第一個

字串 String 一些好用的 function, scan, split, match, gsub, sub

Scan


Split

Match and Scan 感受一下啥時要用啥吧

gsub and sub 就是 replace 只是一個全部, 一個只有第一個

2009年1月2日 星期五

CSS Tools: Reset CSS

CSS Tools: Reset CSS

The goal of a reset stylesheet is to reduce browser inconsistencies in things like default line heights, margins and font sizes of headings, and so on.

為了減少個瀏覽器間一些 default tag 的差異, 所以有了這些css, 像Blueprint就是css的 framework 讓你省去css development 時間

CSS Tools: Reset CSS

CSS Tools: Reset CSS

The goal of a reset stylesheet is to reduce browser inconsistencies in things like default line heights, margins and font sizes of headings, and so on.

為了減少個瀏覽器間一些 default tag 的差異, 所以有了這些css, 像Blueprint就是css的 framework 讓你省去css development 時間

Top 10 Ruby on Rails performance tips

Top 10 Ruby on Rails performance tips

1. 最佳化你的ruby code
1.1 如果內建就有, 就別自己幹
1.2 能用regular expression 就別用loop
1.3 用 Libxml 別用 REXML 如果妳要parse xml
1.4 define_method, yield 動態生function 很方便, 可是也要好好評量一下
1.5 儘量別用loop,
1.6 if/unless 可以儘量簡單或別用, 別忘了 ||=
1.7 Hash 很貴, 如果妳有需要常用的話, 把他存在local variable裡


2. Cache is good
2.1 model : cached_model gem
2.2 page : extended_fragment_cache

3. finders are great but be careful
3.1 select , 只拿妳需要的
3.2 別忘了 include
3.3 MyModel.find_by_* 會痛 別用
3.4 想快 就用 find_by_sql

4. Group operations in a transaction:
!! 用transaction 包起來這樣比較快, 如果妳有需要不停的insert or update record 的話


5. Control your controllers
5.1 filter 很貴, 別用太多, instance variable 也是

6. Use HTML for your views:
6.1 helper 別用太爽

7. Logging
7.1 logging 也很貴, 別啥都記

8. Profile 是好物

Top 10 Ruby on Rails performance tips

Top 10 Ruby on Rails performance tips

1. 最佳化你的ruby code
1.1 如果內建就有, 就別自己幹
1.2 能用regular expression 就別用loop
1.3 用 Libxml 別用 REXML 如果妳要parse xml
1.4 define_method, yield 動態生function 很方便, 可是也要好好評量一下
1.5 儘量別用loop,
1.6 if/unless 可以儘量簡單或別用, 別忘了 ||=
1.7 Hash 很貴, 如果妳有需要常用的話, 把他存在local variable裡


2. Cache is good
2.1 model : cached_model gem
2.2 page : extended_fragment_cache

3. finders are great but be careful
3.1 select , 只拿妳需要的
3.2 別忘了 include
3.3 MyModel.find_by_* 會痛 別用
3.4 想快 就用 find_by_sql

4. Group operations in a transaction:
!! 用transaction 包起來這樣比較快, 如果妳有需要不停的insert or update record 的話


5. Control your controllers
5.1 filter 很貴, 別用太多, instance variable 也是

6. Use HTML for your views:
6.1 helper 別用太爽

7. Logging
7.1 logging 也很貴, 別啥都記

8. Profile 是好物

2009年1月1日 星期四

[HTML] IE 表單 Enter 的 Bug

[HTML] IE 表單 Enter 的 Bug

ie 只有一個input box 不給submit 那就在補它一個hide 起來的input box

[HTML] IE 表單 Enter 的 Bug

[HTML] IE 表單 Enter 的 Bug

ie 只有一個input box 不給submit 那就在補它一個hide 起來的input box