0
我有一箇舊的項目,看起來像這樣一類:在模型中使用「puts」有沒有更好的選擇?
class UserObserver < ActiveRecord::Observer
def after_create(user)
unless Rails.env == "development" # don't spam users when running legacy migrations
begin
UserMailer.welcome(user).deliver
subscribe_user_to_new_account_mailinglist user
rescue
# THE FOLLOWING LINE IS THE ONE THAT BOTHERS ME
puts "User##{user.id} - Signed up using an invalid email address."
end
end
end
end
正如你可以看到,有這麼puts
那裏。當我運行我們的測試套件,我得到的輸出是這樣的:
be rspec spec/models/user_spec.rb
User#1 - Signed up using an invalid email address.
...User#1 - Signed up using an invalid email address.
..User#1 - Signed up using an invalid email address.
.User#1 - Signed up using an invalid email address.
.User#1 - Signed up using an invalid email address.
.User#1 - Signed up using an invalid email address.
.User#1 - Signed up using an invalid email address.
.User#1 - Signed up using an invalid email address.
.User#1 - Signed up using an invalid email address.
.User#1 - Signed up using an invalid email address.
.User#1 - Signed up using an invalid email address.
.User#1 - Signed up using an invalid email address.
.User#1 - Signed up using an invalid email address.
.User#1 - Signed up using an invalid email address.
.User#1 - Signed up using an invalid email address.
.
Finished in 12.19 seconds
18 examples, 0 failures
我沒有多餘的雜亂的大風扇。似乎我應該只看到綠點。
在該行上而不是puts
上做什麼更好?
怎麼樣'Rails.logger.info'你的消息''? –
Rails.logger.debug「消息」 – Debadatt
工作。如果有人把'Rails.logger'作爲答案,我會接受它。謝謝。 –