解決過的問題備忘錄 (外部記憶體?)

顯示具有 rails 標籤的文章。 顯示所有文章
顯示具有 rails 標籤的文章。 顯示所有文章

2010年2月23日 星期二

Make acts_as_versioned know target model belongs_to association

acts_as_versioned is a great plug-in for Rails. It can keep track of all modifications to your target model with only a little setup. But it seems not support has_*/belongs_to association by default. So let's see how to enable this.

Assume that you are going to keep track of the 'Post' model. and this is probably your model looks like:
class Post < ActiveRecord::Base
  acts_as_versioned 
  belongs_to :user
end

But version.user will not work (at least, for this version).


(1) For newer version of acts_as_versioned, try this:
class Post < ActiveRecord::Base
  acts_as_versioned do
    belongs_to :user
  end
end

(2) And for older version, try this:
class Post < ActiveRecord::Base
  acts_as_versioned 
  belongs_to :user
end

Post.versioned_class.class_eval do
  belongs_to :user
end


solution 2 works for me! but solution 1 not (ruby 1.8.6, rails 2.3.2, acts_as_versioned 0.3.1)


==Traditional Chinese version==


acts_as_versioned 是個好套件。只要一點設定他就可以幫你紀錄model的版本變動。可是他預設沒有支援has_*/belongs_to的資料庫關聯,所以讓我們來看看要如何打開這個功能。

假設你要追蹤的是Post這個model,那你的model檔大概長這樣:
class Post < ActiveRecord::Base
  acts_as_versioned 
  belongs_to :user
end


但這個時候 version.user 是會掛掉的(至少這版套件如此)


(1) 套件版本較新者可以試試這個:
class Post < ActiveRecord::Base
  acts_as_versioned do
    belongs_to :user
  end
end


(2) 舊版者,則試這個:
class Post < ActiveRecord::Base
  acts_as_versioned 
  belongs_to :user
end

Post.versioned_class.class_eval do
  belongs_to :user
end



我用第2個方案OK,但是第1個方案則不行... (ruby 1.8.6, rails 2.3.2, acts_as_versioned 0.3.1)

2010年2月20日 星期六

Rails sending emails via gmail not working in heroku server but ok in localhost

I've encountered a problem that my rails app can send email via gmail properly in my computer but not working in heroku server (get an error: "Must issue a STARTTLS command first").

Following by this post:

1. download tls_smtp.rb. (found from this post)
2. put it into your app lib directory
3. import it in config/environment.rb
require 'tls_smtp'

Then upload to heroku, it's done!

I didn't add additional config in config/environments/production.rb as this post said. But it still work.

This is my mailer config (I put it in config/initializers/mail.rb):
ActionMailer::Base.smtp_settings = {
  :enable_starttls_auto => true,
  :address => 'smtp.gmail.com',
  :port => 587,
  :domain => 'gmail.com',
  :authentication => :plain,
  :user_name => 'xxxxxx@gmail.com',
  :password => '******'
}

Hope this helps


==Traditional Chinese version==

我剛才遇到一個問題,我用gmail寄信,在localhost也測試OK,但是丟上heroku後卻壞掉(得到「Must issue a STARTTLS command first」錯誤)

後來找到了一篇文章,依照指示:

1. 下載 tls_smtp.rb. (在這篇找到的)
2. 放到app的lib資料夾
3. 引入到 config/environment.rb
require 'tls_smtp'

然後上傳到heroku,完成了!

我沒有依照這篇所說要再增加mailer設定到config/environments/production.rb,不過他還是成功。

這是我的mailer設定 (我放在 config/initializers/mail.rb):
ActionMailer::Base.smtp_settings = {
  :enable_starttls_auto => true,
  :address => 'smtp.gmail.com',
  :port => 587,
  :domain => 'gmail.com',
  :authentication => :plain,
  :user_name => 'xxxxxx@gmail.com',
  :password => '******'
}

希望這篇有幫助

2008年10月21日 星期二

an acts_as_versioned error/bug with Rails 2.1 -在rails 2.1用acts_as_versioned插件奇怪的錯誤

use acts_as_versioned plug-in lead to an unknown exception:

"NoMethodError: You have a nil object when you didn't expect it!"

from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/attribute_methods.rb:211:in
`version='
from /Users/toby/rorbar/olhc_website/vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb:431:in
`send'
from /Users/toby/rorbar/olhc_website/vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb:431:in
`set_new_version'

It is maybe because of out-of-day acts_as_versioned plugin with rails 2.1 or above.

The partial updates feature in 2.1 caused a conflict with the the way
acts_as_versioned handles it's own change history. Check out the
latest master on github as this has been resolved:
http://github.com/technoweenie/acts_as_versioned/tree/master
see http://www.nabble.com/acts_as_versioned-and-Rails-2.1-td17665890.html

To solve this. reinstall the acts_as_versioned gem. or download tarbal/zip package from GitHub(link above) and unzip into the rails application's vendor/plugin directory.
(i have problem in re-installation. it always install only Docs. even give a -f augment Do anyone know how to re-install a gems plug-in?)



==Chinese version==

使用
acts_as_versioned插件時導致了以下的錯誤

"NoMethodError: You have a nil object when you didn't expect it!"

from /opt/local/lib/ruby/gems/1.8/gems/activerecord-2.1.0/lib/active_record/attribute_methods.rb:211:in
`version='
from /Users/toby/rorbar/olhc_website/vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb:431:in
`send'
from /Users/toby/rorbar/olhc_website/vendor/plugins/acts_as_versioned/lib/acts_as_versioned.rb:431:in
`set_new_version'
這可能是因為您在rails 2.1或更新的版本中使用老舊的acts_as_versioned插件

The partial updates feature in 2.1 caused a conflict with the the way

有一部分rails 2.1的新功能導致acts_as_versioned插件處理歷史紀錄功能壞掉
acts_as_versioned handles it's own change history. Check out the
latest master on github as this has been resolved:
請從github弄出最新的程式碼,master已經修正:
http://github.com/technoweenie/acts_as_versioned/tree/master
請參考 http://www.nabble.com/acts_as_versioned-and-Rails-2.1-td17665890.html


要解決這個問題,重新安裝
acts_as_versioned插件,或直接從github網站下載tarbal/zip的壓縮檔,解壓縮到rails目錄的vendor/plugin子目錄中
(但是我重新安裝有問題,即使我下了-f參數,他還是只會安裝新的文件工具,有人知道要如何確實重新安裝gems的插件嗎?)

2008年9月23日 星期二

ror rails mailer with sendmail don't work? 不會寄信 寄不出去? mailer setup設定 RESTful Authentication mailer

the main point is the augment of sendmail setting. if you did not set the augment -i -t, the sendmail server will wait rather then send immediately. it's because the usage of sendmail server. you can search the sendmail server manual if you want.

by the way, if you want to use RESTful Authentication with activation. you may run into a big question about 'why did the mailer not work?'. this is one of the reason. but if it still can't work. check if you already active the observer which you can set in [config/environment.rb]. (try find those keywords: config.active_record.observers)

in Chinese:
主要的點是你必須設定sendmail的參數,如果沒有設-i -t的話,sendmail server會等待不發送,這跟sendmail server的用法有關,有興趣的話可以去vbird鳥哥網站找sendmail教學。
另外,如果你是想用RESTful Authentication的activation功能,發現mail沒辦法寄,除了以上說的,還有一個新手可能沒注意到的地方是,observer要在[config/environment.rb]設定檔裏面打開,搜尋一行config.active_record.observers 那邊把相應的observers 的名稱打上去


code:
config.action_mailer.raise_delivery_errors = true

ActionMailer::Base.delivery_method = :sendmail

ActionMailer::Base.sendmail_settings = {
:location => '/usr/sbin/sendmail',
:arguments => '-i -t'
}

reference:
http://www.red91.com/2007/11/05/sending-mail-with-rails

2008年2月19日 星期二

acts as authenticated為什麼無法寫入actived_at欄位?

如果你跟我遇到一樣的狀況
在rails的acts as authenticated套件啟用activation認證碼開通功能的時候
發現activation_code這個欄位會被清除,但actived_at欄位卻還是NULL沒有變成寫入時間(導致登入時被擋掉)
那麼肯定跟我中同一個標了

Be careful !! *
User Activation is not secured. You need to add the following line
to your user model : attr_protected :activated_at


這段取自acts as authenticated的說明網頁
他叫你在user model加入「attr_protected :activated_at」這行

如果你乖乖的加入了
現在請把它刪除

嗯...然後這部份就正常了...


我猜可能是rails版本不同的關係
當時用「acts as authenticated activated_at」當keywords也沒搜尋到資料
總之
我花了23次的try and error才發現的(一切搞定時,table users已經autoindex到23了)

辛苦我自己了= ="

2007年8月30日 星期四

使用acts as authenticated plugin在if current_user判斷出錯

如果跟著「Ruby on Rails專業網站案例實作」這本書走,在第八章安全性應該教到使用acts as authenticated插件,但奇怪的是layout中下面那行總會出錯,拿書商的範例檔來run也一樣。
<% if current_user %>

小弟花了幾天的時間才搞定(小弟不才orz),current_user是在.../lib/authenticated_system.rb 中的一個method,在未登入狀況下會傳回:false,因此將範例原始檔中、出錯的那行改成如下,便可正確執行。
<% if current_user != :false %>
另外一種做法是只將.../lib/authenticated_system.rb 中的current_user method改成如下
def current_user
@current_user ||= (session[:user] && User.find_by_id(session[:user])) || nil #原本最後一個是:false
end
小弟使用前者。