新手问题 新手问题第一季:邮件为啥发不出去?

kenneth · 2012年06月06日 · 最后由 fresh_fish 回复于 2012年06月06日 · 5330 次阅读
Loading development environment (Rails 3.2.5)
irb(main):001:0> UserMailer.test.deliver
ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attribut
es: mail, to, from
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.5/lib/active_mo
del/mass_assignment_security/sanitizer.rb:48:in `process_removed_attributes'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.5/lib/active_mo
del/mass_assignment_security/sanitizer.rb:20:in `debug_protected_attribute_remov
al'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.5/lib/active_mo
del/mass_assignment_security/sanitizer.rb:12:in `sanitize'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activemodel-3.2.5/lib/active_mo
del/mass_assignment_security.rb:230:in `sanitize_for_mass_assignment'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_r
ecord/attribute_assignment.rb:75:in `assign_attributes'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_r
ecord/base.rb:498:in `initialize'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_r
ecord/persistence.rb:44:in `new'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activerecord-3.2.5/lib/active_r
ecord/persistence.rb:44:in `create'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/ar_mailer_rails3-2.1.12/lib/ar_
mailer_rails3/active_record.rb:49:in `block in deliver!'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/ar_mailer_rails3-2.1.12/lib/ar_
mailer_rails3/active_record.rb:48:in `each'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/ar_mailer_rails3-2.1.12/lib/ar_
mailer_rails3/active_record.rb:48:in `deliver!'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/mail-2.4.4/lib/mail/message.rb:
2034:in `do_delivery'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/mail-2.4.4/lib/mail/message.rb:
229:in `block in deliver'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/actionmailer-3.2.5/lib/action_m
ailer/base.rb:415:in `block in deliver_mail'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.5/lib/active_
support/notifications.rb:123:in `block in instrument'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.5/lib/active_
support/notifications/instrumenter.rb:20:in `instrument'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.5/lib/active_
support/notifications.rb:123:in `instrument'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/actionmailer-3.2.5/lib/action_m
ailer/base.rb:413:in `deliver_mail'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/mail-2.4.4/lib/mail/message.rb:
229:in `deliver'
        from (irb):1
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/comman
ds/console.rb:47:in `start'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/comman
ds/console.rb:8:in `start'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/comman
ds.rb:41:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

fields being mass assigned are protected perhaps?

代码里应该如何写,才能行?不想 config.active_record.whitelist_attributes = false

look up attr_protected and attr_accessible they should be placed in ur models

这个:mail,:to,:from 是 mailers 里面的,不是 models 里面,不知道去哪里写?

贴代码

class UserMailer < ActionMailer::Base
  default from: "[email protected]"

  def test
    mail(:to => "[email protected]",:body => "Have a good day!", :subject => "test mail")  
  end

end

我在错误栈看到 ar_mailer_rails3 这个组件,是干什么用的?问题也许出在这里,rails 3.2.* 默认开启了属性白名单,ar_mailer_rails3 是用 ActiveRecord 来模拟什么了吧,需要更新或者修复跟 Rails 的兼容性。

PS:代码块格式化一下,看我对顶楼的处理

ar_mailer_rails3 是把要发的邮件,都存到数据库中。然后我执行一下就异步发出去了。 ActiveModel::MassAssignmentSecurity::Error: Can't mass-assign protected attributes: mail, to, from 不知道如何把这几个属性,添加到白名单去。我看别人的源码,也没在 mailers 里面做啥处理啊。

自己打个补丁,新建一个 config/initializers/ar_mailer_rails3.rb,内容是

module ArMailerRails3
  class ActiveRecord
    attr_accessible :to, :from, :mail
  end
end

自己打补丁后报的错。

Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.
D:/railsdemo/config/initializers/ar_mailer_rails3.rb:3:in `<class:A
ctiveRecord>': undefined method `attr_accessible' for ArMailerRails3::ActiveReco
rd:Class (NoMethodError)
        from D:/railsdemo/config/initializers/ar_mailer_rails3.rb:2
:in `<module:ArMailerRails3>'
        from D:/railsdemo/config/initializers/ar_mailer_rails3.rb:1
:in `<top (required)>'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.5/lib/active_
support/dependencies.rb:245:in `load'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.5/lib/active_
support/dependencies.rb:245:in `block in load'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.5/lib/active_
support/dependencies.rb:236:in `load_dependency'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.5/lib/active_
support/dependencies.rb:245:in `load'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/engine
.rb:588:in `block (2 levels) in <class:Engine>'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/engine
.rb:587:in `each'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/engine
.rb:587:in `block in <class:Engine>'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/initia
lizable.rb:30:in `instance_exec'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/initia
lizable.rb:30:in `run'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/initia
lizable.rb:55:in `block in run_initializers'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/initia
lizable.rb:54:in `each'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/initia
lizable.rb:54:in `run_initializers'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/applic
ation.rb:136:in `initialize!'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/railti
e/configurable.rb:30:in `method_missing'
        from D:/railsdemo/config/environment.rb:5:in `<top (require
d)>'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.5/lib/active_
support/dependencies.rb:251:in `require'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.5/lib/active_
support/dependencies.rb:251:in `block in require'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.5/lib/active_
support/dependencies.rb:236:in `load_dependency'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/activesupport-3.2.5/lib/active_
support/dependencies.rb:251:in `require'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/applic
ation.rb:103:in `require_environment!'
        from D:/Ruby193/lib/ruby/gems/1.9.1/gems/railties-3.2.5/lib/rails/comman
ds.rb:40:in `<top (required)>'
        from script/rails:6:in `require'
        from script/rails:6:in `<main>'

#10 楼 @kenneth 这个 gem 生产代码是不是生成了一个 model?直接把 attr_accessible :to, :from, :mail 写到那个 model 里面,补丁不要了。

@Rei 谢谢你中下水平的 Rails 程序员,问题已经解决了。

class Email < ActiveRecord::Base
  attr_accessible :to, :from, :mail
end

@Rei 还有你代码注释的引号怎么打的?我总是复制你的,```ruby 前面是不是引号?

#13 楼 @kenneth ~ 号下面的那个符号

谢谢,这个啥语法,难道是 markdown 格式?@Rei 在上海吗?

#15 楼 @kenneth 扩展的 Markdown 语法。

在北京。

#12 楼 @kenneth 看你的回复有点搞笑 人家 ID 是 Rei 嘛,不是 中下水平 Rails 程序员

#17 楼 @fresh_fish 哈,我签名确实是这个,起因是之前 Rails 的 MassAssignment 漏洞事件,提出问题的 hacker 说很多中下水平程序员会忽视这个问题,然后我发现我写的代码也中箭了。

#18 楼 @Rei 看了你的解释后,掩面,改签名......

#18 楼 @Rei 既然大部分人都中招了,那其实只能说 Rails 开发者的框架设计能力处于中下水平。

just test...发现有时候发表回复特慢

需要 登录 后方可回复, 如果你还没有账号请 注册新账号