同步發布在http://www.inside.com.tw

照目前官方支援的情況來看,用php來寫facebook的應用應該是首選,但如果想用ruby on rails寫,也不是不行。這篇文章就拿ruby on rails來介紹fb ap的寫法,並整合正ㄏㄤ的Heroku hosting,如何在四個小時內,無成本的建立出你的facebook ap。

在本篇文章中,一般性的facebook ap注意事項請參考前一篇,本篇只針對ruby on rails特定的部分做介紹。一樣,拿已經寫過的"朋友點點名"為例,來介紹怎麼入手。

ask_friends

0.  首先,還是玩一下"朋友點點名"這個應用!瞭解一下他做了甚麼事情。

1.  介紹一下"朋友點點名"的流程:

首先確認是否授權"朋友點點名"存取你的facebook資料。

授權後,facebook會問你,想取得"自動發佈wall”的權限。

接下來,系統自動選一個你的朋友,顯示他的圖片,並且自動問你一個問題,你需要輸入你的答案。

回答後,你的回答就自動發佈到該朋友的wall上面了。

這裡牽涉到的部分比php寫的那支較多,底下開始快速的介紹:

3. 安裝facebooker plugin

開一個rails專案

rails testproject

在你的rails專案裡,安裝facebooker plugin

cd testproject 
script/plugin install git://github.com/mmangino/facebooker.git

4. 建立heroku帳號與環境

首先,先到 http://www.heroku.com 申請一個帳號

確定申請好了之後,在你的linux環境中,用root執行

gem install heroku  //這樣就可以使用heroku相關指令,遠端管理操作 
 
heroku create testproject //建立heroku上的一個ror執行專案,會問你Heroku credentials,輸入你剛建立的heroku帳號的email跟密碼,這樣就可以建立完成。本例子中, testproject是專案名稱。

上述步驟如果成功完成,畫面上會吐出一串字,類似:

        Created http://testproject.heroku.com/ | git@heroku.com:testproject.git

其中,http://testproject.heroku.com/就是你的網站網址!對於小程式來說,你連domain name都不用申請就可以把ap放到facebook上了。而git@heroku.com:testproject.git這行就是 heroku上幫你開的source code存放的地方了。

接下來,我們把剛剛產生的rails 專案都放上去吧!

前提是,你得會用git,而且有安裝了git。本篇就不描述相關安裝方法。

在testproject rails專案的目錄中

git init 
git add . 
git commit -m "the first time" 
git push git@heroku.com:testproject.git master

至此,你會看到類似這樣的訊息:

Counting objects: 62, done. 
Compressing objects: 100% (55/55), done. 
Writing objects: 100% (62/62), 79.37 KiB, done. 
Total 62 (delta 10), reused 0 (delta 0) 
 
-----> Heroku receiving push 
-----> Rails app detected 
Compiled slug size is 80K 
-----> Launching....... done 
http://testproject.heroku.com deployed to Heroku 
 
To git@heroku.com:testproject.git 
* [new branch]      master -> master

那麼,看一下http://testproject.heroku.com,就看到了最基本的ruby on rails  server已經跑起來了!你有了一個server可以用了!

當然,這樣的使用只能作為實驗等級,大規模運用還須對heroku有進一步的瞭解,並且需要$$。但是我們現在可以用這個免費的資源來做點有趣的事情吧。

5. 到facebook上面建立一個新程式,拿到api key等資料,修改config/facebooker.yml

當裝了facebooker plugin後,專案的config目錄會多一個facebooker.yml的檔案,修改其中 production:的段落

最重要的只有三行

api_key: 填入吧 
secret_key: 填入吧! 
canvas_page_name: /

這樣,準備工作差不多完成了,底下開始寫facebook相關的程式碼了!

6.  讓使用者授權testproject可以存取facebook上他的資料

在controller裡面加上這兩行

ensure_application_is_installed_by_facebook_user 
filter_parameter_logging :fb_sig_friends

7. 隨機挑選朋友

在controller裡面,寫這行。那麼@friends這個 array就是你所有朋友打亂排序的結果。

@friends= (facebook_session.user.friends.sort_by {rand})

8. 顯示朋友圖片

在view裡面,這樣寫:

<fb:profile-pic uid="<%=@friends[0].uid%>" size="normal" />

9. 要求同意自動publish到wall的權限

在view裡面,加上

<script> 
Facebook.showPermissionDialog('publish_stream'); 
</script>

至於跳出一個視窗說要 publish 感想到朋友的 wall上的寫法,請參閱php那篇

10. 重新上傳code到heroku去

專案根目錄下,執行

git add . 
git commit -m "the first time"
git push git@heroku.com:testproject.git master

看到server重新啟動之後,連到你的facebook app試試看囉! http://apps.facebook.com/ask_friend/

11. 遇到問題!

執行的時候難免遇到問題,想查 log的話,打

heroku logs --app testproject  //把testproject 換成你的heroku project名稱

以上,快樂的開始寫facebook ap吧~如果有問題,也歡迎留言討論,教學相長。

是的,好康要分享!連結在http://ho.kun.la/houcan/?id=LKJ-S

免費的Ruby on Rails書籍下載不是天天有,而且這算是合法的!

看了目錄還頗實用,跟Agile那本結構類似,因此可非常容易地互相對照。

Patrick Lenz

By Patrick Lenz

作者Patrick好心的開放六十天給人們免費下載,文章寫作時還有五十四天!

連結在這裡:

1. 該網站:http://www.sitepoint.com/books/rails1/freebook.php

2. 直接下載點:http://media.sitepoint.com/books/ror.pdf

3. 或者,你想用買的:https://sitepoint.com/bookstore/go/96/d86d64/9c8fa4c01f

如果在linux安裝ruby時,您符合以下幾個條件:

1. 從source安裝 ruby

2. 執行某些程式發生類似以下的錯誤訊息,我是使用 ar_sendmail時,

Custom_require.rb:27:in `gem_original_require’: no such file to load — openssl (LoadError)

解決:

1. 確定openssl的 library有安裝,包括devel的也要裝。

libopenssl0.9.8-devel-0.9.8e-2mdv2007.1.i586.rpm
openssl-0.9.8e-2mdv2007.1.i586.rpm
libopenssl0.9.8-0.9.8e-2mdv2007.1.i586.rpm

2. 到原先安裝ruby 的source目錄

cd ext/openssl

ruby extconf.rb
make
make install

好了,大功告成,可以執行了。

http://www.noobkit.com/

這個線上文件庫資料豐富,查找方式也很方便。

如果在linux下run Ruby on Rails的ap看到這個錯誤

那麼

請修改config/database.yml,針對所需要的資料庫,加入
  socket: /var/lib/mysql/mysql.sock

當然,那個mysql.sock的路徑,要視實際環境而訂

shell下下 mysql指令,然後打 status就可看到。

遇到client denied by server configuration: proxy:balancer://mongrel_cluster/error/HTTP_FORBIDDEN.html.var的問題。

Google仍然是最佳的朋友 – 以下是相關有用的資源。

1. http://blog.codahale.com/2006/06/19/time-for-a-grown-up-server-rails-mongrel-apache-capistrano-and-you/

2. http://blog.wishlisting.com/?p=20

我的問題發生在 apache 關於mod_proxy 的設定,

因此必須在vhost設定的部份關於Proxy balancer的部分加上兩行,如下:

<Proxy balancer://mongrel_cluster>
BalancerMember http://127.0.0.1:8000
BalancerMember http://127.0.0.1:8001
Order allow,deny
Allow from all
</Proxy>

這樣就OK了。

1. 拷貝或建立設定檔連結到etc

as root,

mkdir /etc/mongrel_cluster

ln -s /你的rail應用程式路徑/config/mongrel_cluster.yml /etc/mongrel_cluster/自訂名字.yml

2. 拷貝啟動檔到 /etc/init.d

cp /mongrel_cluster gem的路徑/mongrel_cluster_gem/resources/mongrel_cluster /etc/init.d/

*如果不知道在哪裡,可$ find / -name mongrel_cluster -print

3. 修改屬性為可執行,並修改裡面關於USER的內容

chmod +x /etc/init.d/mongrel_cluster

vi /etc/init.d/mongrel_cluster

找到USER= mongrel這行

改成你要跑這個app的user id。

4. 把服務打開

chkconfig mongrel_cluster on

 5. 測試

service mongrel_cluster start

curl -I http://localhost:8001

6. 如果像我一樣,ruby裝在 /usr/local/bin

那麼會需要以下動作,否則服務也起不來,會有/usr/bin/env: ruby: No such file or directory
的錯誤。

ln -s /usr/local/bin/mongrel_rails /usr/bin/mongrel_rails

ln -s /usr/local/bin/ruby /usr/bin/ruby

1. 安裝,as root

$ gem install –include-dependencies mongrel
$ gem install –include-dependencies mongrel_cluster

2. 設定

$ mongrel_rails cluster::configure -e production -p 8000 -a 127.0.0.1 -N 2 -c /source的路徑

-a 聽這個 ip

-N 啟動幾個mongrel instance

-c 啟動的rails程式路徑

3. 啟動 mogrel cluster

mongrel_rails cluster::start

4. 測試有沒有開起來

$ curl -I http://127.0.0.1:8000
HTTP/1.1 200 OK
Connection: close
Date: Fri, 17 Aug 2007 10:31:56 GMT
ETag: “46c0d832-1d80″
Last-Modified: Mon, 13 Aug 2007 22:16:18 GMT
Content-Type: text/html
Content-Length: 7552

 5. 關閉mogrel cluster
mongrel_rails cluster::stop

OK了…

用Aptana作為Ruby on Rails的開發IDE,用了幾個禮拜了,感覺不到比起先前的RaidRails有很大的進步。

現在想建立cvs環境,發現Aptana並無內建支援cvs plugin,google了一下,放棄。改使用他的Sync Manager,透過ftp或sftp的方式來同步資料,做備份或上傳用。

我先備份大約15G的資料,透過ftp傳到server上。結果aptana新開啟的同步預覽java視窗,吃掉了大部分的cpu 資源。

15G的資料透過11M的wireless要傳很久,但我的XP筆記型電腦被這個視窗拖著,不太能作業,滑鼠移動會延遲。因此 ctrl+alt+del,找到拖慢速度的程序(某個javaw),把他的優先權調整為"低",弄成背景在跑,電腦操作速度才恢復正常。

Aptana還有甚多要改進的地方。

安裝 capistrano時,遇到以下問題,google看不到什麼資料。

#gem install –include-dependencies capistrano
ERROR:  While executing gem … (Gem::InstallError)
    invalid gem format for /usr/local/lib/ruby/gems/1.8/cache/needle-1.3.0.gem

試了兩次後,決定rm /usr/local/lib/ruby/gems/1.8/cache/needle-1.3.0.gem

再重新執行gem install,結果就裝好了。

或許是下載的過程中,有什麼傳輸上的閃失吧。