Tuesday, June 30, 2009

Installing Ruby on Rails on Ubuntu

Ref:
  1. Installing Ruby on Rails on Debian/Ubuntu
  2. Debian / Ubuntu Linux 的 Ruby on Rails 環境架設

Chorme in Ubuntu
  1. Install google chrome with wine in Ubuntu
  2. How to Install Chromium (Google chrome) in Ubuntu using deb package
Friday, June 26, 2009

Convert Ruby code to HTML code for blog posting

本篇前傳請看Here。

直接進入主題,首先,要先安裝一個叫syntax的東西(原諒我稱呼他為"東西" 因為我才讀ruby第二天Q口Q)
安裝的方法是透過command line下:

>gem install syntax

執行畫面如下:


安裝完成後就可以來.... 寫formatter程式XD,說寫,其實是剪剪貼貼啦XD 對程式的部分一知半解的。

程式內容如下:
################
## ruby2html.rb
################

require "rubygems"
# "rubygems" 少了就被抱怨:
#`require': no such file to load -- syntax/convertors/html (LoadError)
require "syntax/convertors/html"
require "Win32API"
require "Win32/clipboard"
include Win32
# 上面這行少會被被抱怨:
#undefined method `data' for Windows::Clipboard:Module (NoMethodError)

in_data = Clipboard.data
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
code_html = convertor.convert(in_data)
Clipboard.set_data(code_html)
以上算是try and error來的XD,我也不知道為什麼別人的程式可以跑,我就要修修改改的。
我把檔名存成"ruby2html.rb"丟在/rails_app/下面。
這支程式是這樣用的: 把要format的code選起來按複製,接著就run剛剛存好的程式:

>ruby ruby2html.rb

跑完後command line不會show什麼訊息,這時候只要開個notepad起來按貼上,就可以看到處理好的html code,就完成啦~

最後,還是要配合CSS,CSS的部份我也自己做了些修改,因為之前已經先有一段跟C#有關的CSS,怕兩種code的CSS會互相影響,以下是我的CSS file:
/* ------------------------- For format ruby codes --------------------------*/
.ruby, .ruby pre {
background-color: #f1f1f3;
color: #112;
padding: 5px;
font-family:"bitstream vera sans mono",monaco,"lucida console","courier new",courier,serif;
font-size: 0.9em;
overflow: auto;
margin: 4px 0px;
width: 95%;
}

/* Syntax highlighting */
.ruby .normal {}
.ruby .comment { color: #005; font-style: italic; }
.ruby .keyword { color: #A00; font-weight: bold; }
.ruby .method { color: #077; }
.ruby .class { color: #074; }
.ruby .module { color: #050; }
.ruby .punct { color: #447; font-weight: bold; }
.ruby .symbol { color: #099; }
.ruby .string { color: #944; background: #FFE; }
.ruby .char { color: #F07; }
.ruby .ident { color: #004; }
.ruby .constant { color: #07F; }
.ruby .regex { color: #B66; background: #FEF; }
.ruby .number { color: #F99; }
.ruby .attribute { color: #5bb; }
.ruby .global { color: #7FB; }
.ruby .expr { color: #227; }
.ruby .escape { color: #277; }
/* -----------------------------------------------------------------------*/

嗯,有了CSS跟html code配合起來,就可以把ruby code漂漂亮亮的顯示出來囉~
另外如果覺得每次都用複製code去做很麻煩的話,也可以format整個.rb檔,請自行參閱Ref 4號,個人因為習慣copy片段,所以這個是比較適合自己的方法。

Ref:
  1. Howto format ruby code for blogs (這篇應該算始祖吧..)
  2. Formatting Ruby and HTML code for blog posting
    (這篇參考始祖可以跑... 我參考他就不行跑Q口Q)
  3. syntax ruby code to html in gnome (這篇是成功的關鍵XD)
  4. Syntax Highlighting (一樣的東西,不過他讀整個.rb檔)

ruby code format test

天哪!! 我成功了!! 原本是想說開始學ruby了,也應該像C# code一樣去找個formatter來用,這樣以後要貼code比較好看,結果找來找去,好像ruby本身就提供把code轉成html tags的function!! Ruby真的是programmer的好朋友!!!
in_data = Clipboard.data
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
code_html = convertor.convert(in_data)
Clipboard.set_data(code_html)

過程還是有點卡XD 晚點整理好再來分享~
Wednesday, June 24, 2009

Crystal Report資料存取模式 與 TableLogOnInfo

存取模式詳細說明請見reference。下面只節錄簡易說明:

Crystal Report提拱兩種資料存取模式:
  1. 提取模式
    由報表引擎直接向資料庫提取資料。包括連線還有SQL都由Crystal Report處理。
  2. 推入模式
    由開發人員自行撰寫連接資料庫與撈資料部分,再將資料結果(通常是DataSet)推入報表引擎,再顯示於報表。
當使用提取模式時,就需要另外處理報表連接資料庫時所需要的帳號密碼問題。因為安全考量,Crystal Report並不會記憶連接資料庫的帳號密碼。TableLonOnInfo就是提供來處理這個問題的。

   1:  protected void ConfigCrystalReport(){

   2:      TableLogOnInfo logOnInfo = new TableLogOnInfo();

   3:   

   4:      //load report

   5:      ReportDocument reportDoc = new ReportDocument();

   6:      reportDoc.Load(Server.MapPath("rpt_name.rpt"));

   7:   

   8:      //set up connection info for each table in report

   9:      //if the report connect to not only one table in db

  10:      foreach (CrystalDecisions.CrystalReports.Engine.Table table 

  11:               in reportDoc.Database.Tables)

  12:      {        

  13:          logOnInfo = table.LogOnInfo;

  14:          

  15:          //抓取config檔裡 <AppSettings>區段內設定好的資料

  16:          //除了下面列出的UserID, Password, 還可設定ServerName, DatabaseName

  17:          logOnInfo.ConnectionInfo.UserID = 

  18:                    ConfigurationManager.AppSettings["_UserId"];

  19:          logOnInfo.ConnectionInfo.Password =

  20:                    ConfigurationManager.AppSettings["_Password"];

  21:   

  22:          // apply settings

  23:          table.ApplyLogOnInfo(logOnInfo);

  24:      }

  25:  }


Ref:
Tuesday, June 16, 2009

Use validator and client script at the same time

ㄟ~ 故事是這樣的。
寫web最怕的就是連線不穩定,好死不死我寫的某塊功能就是專給海外的人處理資料用的。最近使用者跟我反應存檔後跑出兩筆一模一樣的資料.... 根據我推論,應該是因為網路剛好不穩,使用者按了submit以為沒反應又按了一次... 網路不穩歸不穩,兩次request還是都處理了。
就是這樣,所以要想辦法防止這次的request完成前再送一次。

聽起來超簡單,就按一下submit的時候把按鈕disable,複雜一點的,按一下之後先跟使用者做個確認,確定了再disable。
嗯,沒錯。就這樣,reference都不完全符合我要處理的狀況,不過頗有參考價值。
還是自己搞定了Q口Q

首先,原本寫在LinkButton上的onClientClick就不能直接寫了,因為需要多一層判斷,所以要在程式裡動態幫他把attribute加上去。

   1:  <!--原本在html裡的client confirm不能用-->

   2:  <asp:LinkButton runat="server" ID="lbSave" CommandName="save" Text="Save"

   3:  CommandArgument='<%#DataBinder.Eval(Container,"RowIndex")%>'

   4:  OnClientClick="return confirm('Are you sure?');">

   5:  </asp:LinkButton>

   6:   

   7:  <!--拿掉後: -->

   8:  <asp:LinkButton runat="server" ID="lbSave" CommandName="save" Text="Save"

   9:  CommandArgument='<%#DataBinder.Eval(Container,"RowIndex")%>'>

  10:  </asp:LinkButton>



我在Gridview資料繫結時才去幫他加上onclick的屬性:

   1:  //加上onclick attritube, 叫用lockBtn的script

   2:  ((LinkButton)GridView2.Rows[i].FindControl("lbSave"))

   3:  .Attributes.Add("onclick", "return lockBtn(this);");



script在這:

   1:  <script language="javascript" type="text/javascript">

   2:      //lockBtn: 接受觸發者當參數

   3:      function lockBtn(btn){    

   4:          //先判斷是否已disable,因為linkBtn就算disable了(會變灰色)

   5:          //但是click一樣會觸發script

   6:          if(btn.disabled==true){

   7:              return false;  

   8:          }

   9:          else{

  10:              //取得使用者確認

  11:              var sure = confirm('Are you sure');

  12:              if(sure==true){

  13:                  //直接disable掉

  14:                  btn.disabled=true;

  15:                  return true;

  16:              }

  17:              else{

  18:                  return false;

  19:              }

  20:          }

  21:      }

  22:  </script>



就這樣。好像沒看到什麼時候解開XD? 因為我原本就有在code裡根據其他數據處理判斷是否enable。所以不必另外處理,每次page_load都會重新設定。搞定。


測試的話,以上面例子來說,動作是在save command裡,可以加個

   1:  System.Threading.Thread.Sleep(5000);


來觀察disable狀態,也可以趁機多按幾次按鈕看是否真的無作用。

Ref:
Friday, June 12, 2009

待研究主題: SQL語法之 IN vs. EXISTS

兩篇討論from Ask Tom:

Tags