Google自訂搜尋

目前分類:Ruby on Rails (22)

瀏覽方式: 標題列表 簡短摘要

Okay.. 剛剛嘗試將number_to_tw_phone給寫成Rails Plugin..發生了一點小插曲,也許是電腦秀逗吧,反正現在是成功了
就讓我紀錄一下吧

  1. 開啟Console
  2. 產生一個Rails Project
  3. 在Rails Project Root Directory中輸入script/generate plugin taiwan_helpers
  4. 此時會產生一個Plugin的資料夾在RAILS_ROOT/vendor/plugins/中,叫做taiwan_helpers
  5. 裡面有很多資料夾,我們看init.rb、libs/*這些就好

init.rb是當Rails啟動Server或Console時會先載入的一個檔案,所以要在這個檔案中將該引入與Mix-in的東西寫在裡面
所以我們先編輯init.rb

require 'taiwan_helpers'
ActionView::Base.send(:include, ActionView::Helpers::TaiwanHelper::NumberHelper)

存檔離開後,開始編輯libs/taiwan_helpers.rb
存檔離開後,啟動Rails Server,寫一段小程式看有沒有成功

接著,如果要把檔案給扔到Github上的話,只要把plugins底下的資料夾完整扔上去即可

TaiwanHelpers Github Page: http://github.com/cfc/taiwan_helpers/tree/master

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(15)

  def number_to_tw_phone(phone, cellphone=false)
    raise "Phone number error!" if phone.size < 9 || phone.size > 10 || (phone.size < 10 if cellphone)
    no = phone.split("")
    return %{#{no[0..3].join}-#{no[4..6]}-#{no[7..9]}} if cellphone
    return %{(#{no[0..2].join})#{no[3..5]}-#{no[6..8].join}} if no[0..2].join == "089" # Tai-dong
    return %{(#{no[0..1].join})#{no[2..4]}-#{no[5..8].join}} if phone.size == 9
    return %{(#{no[0..1].join})#{no[2..5]}-#{no[6..9].join}} if phone.size == 10
  end
  puts number_to_tw_phone("0987654321", true) #=> 0987-654-321
  puts number_to_tw_phone("0234567890")         #=> (02)3456-7890
  puts number_to_tw_phone("087654321")           #=> (08)765-4321
  puts number_to_tw_phone("089876543")           #=> (089)876-543 #=> 這是台東的電話

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(14)

剛剛在解決一個小Bug
用瀏覽器瀏覽某個會丟301的網站時,在Ruby或Telnet都會丟500回來
什麼鬼.. 怎麼會這樣?

其實這是因為沒有User-Agent的關係啦
有些Web Server可能會Reject一些Header中沒有User-Agent的Request
所以這時候只要在丟request時加上User-Agent這個Header即可
原本的原始碼:
response = Net::HTTP.get_response(URI.parse(uri_str))
改成:
uri = URI.parse(uri_str)
http = Net::HTTP.new(uri.host)
response = http.send_request('GET', uri.request_uri, {"User-Agent" => "Mozilla/5.0"})
這樣一來不管是301、302,還是最該死的404都沒問題啦XD

(( 因為這篇是工作上的心得,所以只好擺在Rails啦XD

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(742)

Rails 2.1推出了令人心癢癢的功能喔!讓我感覺到Rails更迷人更方便了>///<
不過我就先說兩個部份吧?

Find:
呵呵,find應該很多人都會用到吧?
這次做了點小更動呢!

假設我有一個Project Model...
  • Project.all #=> Project.find(:all)
  • Project.first #=> Project.find(:first)
接著是新增的喔!
  • Project.find(:last) #=> Project.find(:first, :order => "id DESC")
  • Project.last #=> Project.find(:last)
如何?取得資料更容易了對吧?
Named Scope:
至於這個則是從一個叫做has_finder的gem改過來的一個功能
這個功能被改過來後就改名為named_scope
假設我有一個Model叫做Article...
我想要取得所有public為true的文章,我通常會寫成
Article.find(:all, :conditions => ["public = true"])
或者是
Article.find_by_public(true)
但是透過named_scope...
# In Article.rb
class Article < ActiveRecord::Base
  named_scope :published, :conditions => { :public => true }
end

# In articles_controller.rb
class ArticlesController < ApplicationController
  def index
    @articles = Article.published
  end
end
如此即可!

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(763)

今天摸會了Git,就順便應用上了
error_messages_for大家都用過,也都知道這個不管再怎樣中文化,欄位名稱一樣都會出現給你看!
這真的是令人又愛(英語體系者愛)又恨(非英語體系者恨)的功能啊..
沒辦法,只好自己動手了...
我剛剛發了Git pull給Rails團隊,他們接受不接受我不知道,所以在這邊教大家如何自己搞定這一切
首先,先打開Rails這部份的原始碼
假設我Ruby安裝在C:\
所以路徑就是:C:\ruby\lib\ruby\gems\1.8\gems\actionpack-2.1.0\lib\action_view\helpers\active_record_helper.rb
接著,跳到error_messages_for那段程式碼,在options = params.extract_options!.symbolize_keys底下加入:fields = options[:fields].nil? ? {} : options[:fields]
然後把error_messages = objects.sum {|object| object.errors.full_messages.map {|msg| content_tag(:li, msg) } }.join這行註解,改為:
error_messages = objects.sum {|object| object.errors.full_messages.map {|msg|
              unless fields[msg.split(" ")[0].downcase.to_sym].nil?
                msg = msg.split(" ")
                field_name = msg.shift.downcase!
                msg = msg.reverse.push(fields[field_name.to_sym]).reverse.join(" ")
              end
              content_tag(:li, msg)
} }.join
存檔離開,然後這樣用:
error_messages_for(:project, :fields => {:name => "專案名稱", :summary => "專案摘要"})
而content_tag產生出來的就會是
<li>專案名稱 can't be blank</li><!-- 或其他的錯誤訊息 //-->
<li>專案摘要 can't be blank</li><!-- 或其他的錯誤訊息 //-->
很簡單吧:P?
注意,只能夠傳小寫的symbol進去
沒辦法,我功力太差了=_=|||
可以參考這邊:http://github.com/cfc/rails/commit/9e38903fd10a2de9ae9c2ca53623469f3575b43c
有任何問題歡迎提出,也可以在github上commit給我
多謝多謝:P

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(813)

想看看Rails 2.1有哪些特棒的功能嗎?
在Agile Web Development with Rails 3出來之前,難道只能夠慢慢的爬別人寫的articles嗎?
不用!在這邊提供一個免費且完整度高的Rails 2.1文件!
原文是葡萄牙文的,網址是:
http://www.nomedojogo.com/2008/06/06/o-primeiro-livro-sobre-rails-21-e-brasileiro/

如果沒興趣或看不懂葡萄牙文的,可以看看英文版:http://www.nomedojogo.com/2008/06/09/new-free-book-ruby-on-rails-21-whats-new/

By the way, Rails 2.1更方便了呢!!

Update(2008-06-23):
大陸的Iceskysl前輩前些日子號召了一些人進行中文版的翻譯
翻譯文章已經完成,網址是:http://sites.google.com/site/iceskysl/acticles/ruby-on-rails-21%E6%96%B0%E7%89%B9%E6%80%A7%E4%BB%8B%E7%BB%8D/introduction
感謝Iceskysl與其他的前輩們:D

zusocfc 發表在 PIXNET 痞客邦 迴響(0) 引用(0) 人氣(811)

1 2 3 4