Google自訂搜尋

目前日期文章:200706 (4)

瀏覽方式: 標題列表 簡短摘要
太久沒有寫文章了.. 最近接到一個案子.. 剛好讓我重溫Select的使用方法..
嗯.. 結果卡在multiple,不知道是我太想睡還是怎樣.. 居然傻了..
跑去#rubyonrails問,一位名為carpet_the_walls的網友給了我他寫的文章,網址是:
http://shiningthrough.co.uk/Select+helper+methods+in+Ruby+on+Rails
在此先謝謝carpet_the_walls (Thank you, carpet_the_walls)

來做個Memo.. 不然又忘記了..
在Rails中真的有一堆Select helper可以用.. 不只carpet_the_walls混淆,連我也模糊不清!
常見的有三個..
select, select_tag, collection_select(其餘的什麼select_date那些不談)
我們先來看看一個基本的下拉式選單骨架
<select name="selection">
  <option value="1">Opt1</option>
  <option value="2">Opt2</option>
</select>
在一個下拉式選單中,有一些是必備的資訊,像是"name"、"value"與"text"三個,在回傳資訊給Server時,"name"將是接收資訊用的,而"value"會傳回被選的值,而"text"則是使用者會看到的字,依上面的例子來講,Opt1、Opt2這兩個就是屬於"text"

開始講講那三種Select helper

select:
  select(object, method, choices, options = {}, html_options = {})
  在ActionView::Helpers::FormOptionsHelper中定義

  • object是一個實體化變數,這裡很明顯的就是要擺上model物件嘛!
  • method則是object的一個屬性,也是資料表中的對應欄位
  • choices就是要被選的選項,可以是陣列或者是雜湊(Hash)
  • options與html_options則是一些選項
在這邊來舉個例子吧
<%= select("project", "teacher_id", @teachers.collect{|t| [t.name, t.id]}, { :include_blank => false }) %>
<%= select("project", "student_id", {"CFC" => '1', "EF" => '2'}) %>
第一個例子中,@teachers在Controller是這樣的
@teachers = Teacher.find(:all, :select => 'id, name')

select_tag:
  select_tag(name, option_tags = nil, options = {})
  在ActionView::Helpers::FormTagHelper中定義

如果你很喜歡手動打option的話.. 那用select_tag準沒錯啦!
在select_tag中,name將會是params所接收值所用的鍵
直接看範例
<%= select_tag 'user', "<option>CFC</option>" %>
這時在Controller中將會用params[:user]來接收傳過來的值
但是select_tag也可以搭配options_for_select或者options_from_collection_for_select一起使用.. 來看一個範例吧
<%= select_tag('sid[]', options_from_collection_for_select(@students, 'id', 'name'), :multiple => true)%>
因為加上了:multiple,所以可以接受多值選擇,這時在Controller接收到的sid將會是一個陣列,這也是我所卡住的地方.. (( 真丟臉

collection_select:
  collection_select(object, method, collection, value_method, text_method, options = {}, html_options = {})
  在ActionView::Helpers::FormOptionsHelper中定義

如果資料來源是從資料庫來的話,可以使用這個來做下拉式選單。
這個Object不用我說,就是你的model
method呢?當然就是欄位啦
其實嚴格說起來,這只是select+options_from_collection_for_select的組合啦!
範例:
<%= collection_select(:payment, :id, @payments, :id, :name, options ={:prompt => "-Select a continent"}, :class =>"payment") %>

再次謝謝原作者carpet_the_walls:)

hechian 發表在 痞客邦 留言(0) 人氣()

這個Library搭配http://www.twsms.com才可以使用喔!
有任何問題可以直接回這篇文章,或者寫信問我,我信箱是Gmail的,帳號跟我的這個部落格帳號一樣

我先說用法好了:
require 'twsms'
sms = TWSMS.new(username, password) # 帳號密碼
sms.sendSMS(mobile, message) # mobile: 目標手機號碼  message: 要傳的訊息
原始碼:
=begin
  == Information ==
  === Copyright: Apache 2.0
  === Author: CFC < zusocfc@gmail.com >
  === Prog. Name: TWSMS lib
  === Version: 0.1
  == Introduction ==
    TWSMS(Taiwan SMS)
    TWSMS is a SMS sender, it must use with http://www.twsms.com.
    There has no any library for the SMS system in Taiwan. So, I just coded this and release this version.
    This version just support for sending SMS.
  == Featured ==
   
  == Using TWSMS ==
    It just support for standalone class now.
    require it before you use.
  === Using TWSMS by standalone class
    require 'twsms'
    sms = TWSMS.new('username', 'password')
    sms.sendSMS('09xxxxxxxx', 'Hi, there! TWSMS library is so easy to use!')
    sms.sendSMS('09xxxxxxxx', 'Send SMS with options',
        :popup => 1,
        :type => "now",
        :mo => "Y")
=end

%w|uri cgi net/http|.each{|r| require r}

class TWSMS
  def initialize(username, password)
    @uname, @upwd = username, password
    @options = {
      :type => "now", # Sending type: now, vld
      :popup => "",
      :mo => "Y".upcase,
      :vldtime => "86400",
      :modate => "",
      :dlvtime => "",
      :wapurl => "",
      :encoding => "big5"
    }
   
    @errors = {
      -1.to_s.to_sym => "Send failed",
      -2.to_s.to_sym => "Username or password is invalid",
      -3.to_s.to_sym => "Popup tag error",
      -4.to_s.to_sym => "Mo tag error",
      -5.to_s.to_sym => "Encoding tag error",
      -6.to_s.to_sym => "Mobile tag error",
      -7.to_s.to_sym => "Message tag error",
      -8.to_s.to_sym => "vldtime tag error",
      -9.to_s.to_sym => "dlvtime tag error",
      -10.to_s.to_sym => "You have no point",
      -11.to_s.to_sym => "Your account has been blocked",
      -12.to_s.to_sym => "Type tag error",
      -13.to_s.to_sym => "You can't send SMS message by dlvtime tag if you use wap push",
      -14.to_s.to_sym => "Source IP has no permission",
      -99.to_s.to_sym => "System error!! Please contact the administrator, thanks!!"
    }
    @args = []
    @url ||= "http://api.twsms.com/send_sms.php?"
    @url += "username=" + @uname
    @url += "&password=" + @upwd
  end
 
  def sendSMS(mobile, message, opt={})
    @options[:mobile], @options[:message] = mobile, message
    @options.merge!(opt).each{|k, v| @args << k.to_s + "=" + CGI::escape(v.to_s)}
    @url += "&" + @args.join("&")
    self.chk_val
    chk_errors(Net::HTTP.get(URI.parse(@url)))
  end
 
  def chk_val
    @options[:dlvtime] = "" unless @options[:type] == "dlv"
    @options[:wapurl] = "" if @options[:type] != ("push" && "upush")
  end
 
  def chk_errors(resp)
    resp = resp.split("=")[1]
    if @errors.has_key?(resp.to_s.to_sym)
      puts "==========", "Error!! Message: ", @errors[resp.to_s.to_sym]
    else
      puts "==========", "Message has been send! Your message id is: " + resp.to_s
    end
  end
 
  protected :chk_val
end
晚點丟到Google Code Hosting上去...

Updated:
TWSMS on Google Code Hosting: http://code.google.com/p/twsms/
SMSender on RubyForge: http://rubyforge.org/projects/smsender/

hechian 發表在 痞客邦 留言(2) 人氣()

請在model內加入:
def self.find_by_randomize
  ids = self.find(:all, :select => [id])
  self.find(ids[rand(ids.size)]["id"].to_i)
end
這樣一來,就可以取亂數選取資料了!

請參考這篇:為你的 Active Record 做出多采多姿的 find

當然囉.. thegiive那個就是我修改的範本:P

hechian 發表在 痞客邦 留言(0) 人氣()

呼.. 真是感動
我終於把這東西寫好了~"~
MSXML還真是不好掌握.. 但是YahooMessenger提供的SDK還真... 真難用 囧"
而且.. 為什麼我不能使用IFriend這個介面ˋˊ!!
只有Messenger2可以new.. 囧"

好啦.. 不抱怨了..
圖片:


Twitter帳號是可有可無的,如果沒有輸入則會跑去抓public_timeline
忙碌中要是打勾,則會顯示忙碌中的圖示
要顯示的數量就是輪撥的item數量
狀態更新時間就是每隔幾秒換一次狀態
按下Get會開始抓取XML並且進行輪撥,點擊停止輪撥後就會停止切換狀態
點一下C0d3d by CFC,會跳出About畫面
Exit還需要我說嗎? 囧"

下載點:http://willh.org/cfc/cfc_priv/twitter/tsc.rar
原始碼:http://willh.org/cfc/cfc_priv/twitter/tsc_src.rar

Coded in Visual Basic 6

hechian 發表在 痞客邦 留言(4) 人氣()