2011年6月1日星期三

遇到CSRF的警告訊息

我在create或者update resource時,都不會有問題,可是每次delete時,就會要我重新登入,從Log中會看到一段警告訊息:

WARNING: Can't verify CSRF token authenticity


這個訊息可以參考 ActionController::RequestForgeryProtection 的文件。

解決方式就是在view中加入:

<%= csrf_meta_tags %>

2011年5月28日星期六

Deploy Rails 3.1.0.RC1 to Heroku

Rails 3.1.0.RC1 deploy到heroku時,需要注意兩個部分:

1. 你需要在Gemfile中加入:

group :production do
gem 'therubyracer-heroku', '0.8.1.pre3'
gem 'pg'
end

Heroku的資料庫是使用PostgreSQL,所以需要pg這個gem。
因為原本的therubyracer gem,因為V8的問題,所以無法正常的在heroku中被安裝,所以有人專為heroku改了一個。

2. 你需要在Heroku中執行MRI RUBY 1.9.2

在config/initializers/session_store.rb中


Wiper::Application.config.session_store :cookie_store, key: '_myapp_session'

其中 key: '_myapp_session' 是ruby 1.9的語法,所以heroku中預設的ree 1.8.7會有錯誤訊息。

在Heroku中使用MRI 1.9.2你需要執行:

heroku stack:migrate bamboo-rmi-1.9.2

然後重新push一次。

當然,你也可以不要使用MRI 1.9.2,只需將config/initializers/session_store.rb中改為:

Wiper::Application.config.session_store :cookie_store, :key => '_myapp_session'

不過Rails 3.1已經預設使用Ruby 1.9了,這樣的例子未來可以層出不窮。

參考:

Get Rails 3.1beta1 on Heroku
therubyracer-heroku

2011年5月27日星期五

Rails 3.1 rc1的安裝問題

看到Rails 3.1 出RC1了,當然是興致勃勃的就要來試一下,而Rails 3.1建議是用Ruby 1.9的,所以當然是用RVM來將ruby改為1.9.2。

安裝rails 3.1 RC1基本上只需要一個command:

gem install rails --pre

但是,會有錯誤訊息:

Installing RDoc documentation for activesupport-3.1.0.rc1...
ERROR: While generating documentation for activesupport-3.1.0.rc1
... MESSAGE: incompatible character encodings: UTF-8 and ASCII-8BIT
... RDOC args: --op /Users/weijen/.rvm/gems/ruby-1.9.2-p136/doc/activesupport-3.1.0.rc1/rdoc lib --title activesupport-3.1.0.rc1 Documentation --quiet

其實這只是在RDOC安裝時的錯誤訊息,並不會影響到我測試Rails 3.1 RC1。但是如果你跟我一樣,覺得這個錯誤訊息會搞的你心裡癢癢的,那你可以利用下列的方式改善:

gem update --system (我的ruby 1.9.2p136預設的gem是1.3.7,你需要升級到1.8.x)
gem install rdoc

這樣你就可以完成Rails 3.1的安裝了

2011年4月24日星期日

設定eruby的shiftwidth與softtabstop

在~/.vim/autoload/rails.vim中找到

elseif ft == 'eruby' 這一行程式,然後在該block中加上下列兩行:

call self.setvar('&shiftwidth',4)
call self.setvar('&softtabstop',4)

2011年1月21日星期五

重灌我的Mac

Compile MacVim

下載source code: git clone git://github.com/b4winckler/macvim.git
cd macvim/src
./configure --with-features=huge --enable-rubyinterp --enable-cscope --with-macarchs=x86_64 --with-macsdk=10.6
make
open MacVim/build/Release



Monitor目前cpu/ram...的使用狀況
MenuMeters

2010年9月9日星期四

在Ruby中定義interface

在Java中的Interface,每一個使用的類別,都需要實作在Interface中的method。在Ruby中,我們怎麼做到這件事呢?

假設我們定義一個運動員的module,稱為Athlete。而各種運動的運動員都要實作在Athlete中的method,我們可以這樣做:

如果我們定義一個棒球員的class,使用了Athlete module,卻沒有進行實作,就會有NotImplementedError Exception。