Archive for the ‘Ruby on Rails’ Category

< %...% > v.s < %...-% >

Tuesday, August 21st, 2007

這兩個有什麼不同?

<% ... %>

執行後印出內容,包括你原始碼的空白與換行。

<% ... -%>

除了印出內容外,會削掉多餘的換行。

拿個例子來解釋:

RUBY:
  1. <% 2.times do %>
  2.  
  3.   <li> hello world </ li>
  4.  
  5. <% end %>

輸出的html結果是:
---
空一行
hello world
空一行
空一行
hello world
空一行
空一行
---

改成<% 2.times do -%>
結果是:
hello world
hello world

UTF-8 or BIG5 ? That is a question.

Sunday, August 19th, 2007

Well, I hate being bothered by these encoding issues!
I develop a ror website with my TextMate. And the problem is always client's favorite encoding characters: Big-5. Yes, I love Chinese characters. However, TextMate encode rhtml file with UTF-8. Though I could type well Chinese words on the file.
After browser's gestures, those letters are broken all the time! So, I must code with Textmate AND write rhtml with Dreamweaver. It make me confused sometimes. @@

Not only database collation is big-5 but also the file database.yml has to add line:
encoding:Big5

Difference FormHelpers

Sunday, August 5th, 2007

Read a clarifying explanation for these.

Ruby/Rails file exporting

Sunday, August 5th, 2007

Due to my working project, I have to export some popular file format for the clients to download. Like PDF,Excel,Word ... , I made some surfing and I found:

Get your http header infomation

Saturday, August 4th, 2007

ActionController has a key attribute:
"request"
which contains all http header attributes.

i.e:

def server_ip
location = request.env["SERVER_ADDR"]
render :text => "This server hosted at #{location}"
end

To get root host url, type: request.env["HTTP_HOST"]

more useful key attributes in ActionController:

  • request
  • params
  • session
  • response

RubyAMF 1.0

Sunday, July 8th, 2007

rubyamf logo
官方網站
顧名思義,ActionScript Message Format(AMF) 對 Ruby 的Gateway, 如先前提過的Weborb作用相同。藉由RubyAMF直接傳遞AS物件到伺服器端,能傳遞的物件也彈性的多,否則,傳統的方法都是用字串、XML等來作資料交換。

我寫下簡單的安裝方法:

(more...)

Rails Deprecations

Wednesday, July 4th, 2007

Go Deprecations at rubyonrails.org
由於 Ruby on Rails 正在快速發展中,推陳出新之後,陳舊的語法就必須拋棄或改寫,除此之外,也要注意書本上的寫法,是不是已經過時了。

Ruby Collecting Hash Arguments

Monday, July 2nd, 2007

[reference: Programming Ruby 2nd]
Some languages feature keyword arguments—that is, instead of passing arguments in a given order and quantity, you pass the name of the argumentwith its value,in any order.
Ruby 1.8 does not have keyword arguments. In the meantime, people are

using hashes as a way of achieving the same effect.


class SongList
  def create_search(name, params)
  # ...
  end
end

list.create_search("short jazz songs",
  {
    'genre' => "jazz",
    'duration_less_than' => 270
  })

The first parameter is the search name, and the second is a hash literal containing search parameters. The use of a hash means we can simulate keywords: look for songs with a genre of “jazz” and a duration less than 4 12 minutes. However, this approach is slightly clunky, and that set of braces could easily be mistaken for a block associated with the method. So,

Ruby has a shortcut. You can place key => value pairs in an argument list

, as long as they follow any normal arguments and precede any array and block arguments.
All these pairs will be collected into a single hash and passed as one argument to the method. No braces are needed.

list.create_search('short jazz songs',
   'genre' => 'jazz',
   'duration_less_than' => 270)

Finally, in idiomatic Ruby you’d probably use symbols rather than strings, as symbols
make it clearer that you’re referring to the name of something.

list.create_search('short jazz songs',
   :genre => :jazz,
   :duration_less_than => 270)

A well-written Ruby program will typically contain many methods, each quite small,
so it’s worth getting familiar with the options available when defining and using Ruby
methods.

Merb - 另一個輕量MVC的Framework

Sunday, July 1st, 2007

About Merb

安裝:(Ruby Gem )
$ gem install merb -y

建立專案:
$ merb -g myapp

Merb的檔案結構:

        merb_app:
          Rakefile
          README
          scripts
          test
            spec
            unit
          plugins
          dist
            app
              controllers
              models
              views
            conf
            lib
            public
            plugins
            schema

Typing Chinese Character in Textmate

Saturday, June 30th, 2007

Well, It was a disaster before. Trying to type chineses characters quickly kills your processes. I have encountered this situration so many time. So I was very carefully writing codes.
But, someone came to solve it.

  1. download this font first
  2. download this textmate plugin
  3. install the font( simply place font file into
    library/font
  4. unzip the plugin archieve , double-click
     CJK-Input.tmplugin 
  5. relanuch TextMate
  6. go
    Preference/font&color ,change the font to 'TextMateJ'
  7. done, be happy to type chinese!

Plugin author's website