ruby-on-rails – 神秘的ruby语法错误

前端之家收集整理的这篇文章主要介绍了ruby-on-rails – 神秘的ruby语法错误前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我超乎困惑:这几乎是一个RoR操作邮件程序指南的复制/粘贴,但它会引发语法错误
class Contact < ActionMailer::Base


  def contact a_name,a_company,a_phone,a_email,a_comments
    subject    "Contact request from #{name}"
    recipients "some@address.com"
    from       "some_other@address.com"
    sent_on    Time.now
    body       { :name => a_name,:company => a_company,:phone => a_phone,:email => a_email,:comments => a_comments }
  end

end

错误是:

app/models/contact.rb:9: Syntax error,unexpected tASSOC,expecting '}' body { :name => a_name,:company => a_company... ^ app/models/contact.rb:9: Syntax error,expecting tCOLON2 or '[' or '.' ...{ :name => a_name,... ^ app/models/contact.rb:9: Syntax error,expecting tCOLON2 or '[' or '.' ...ompany => a_company,:... ^ app/models/contact.rb:9: Syntax error,expecting tCOLON2 or '[' or '.' ...,:comments => a_comme... ^ app/models/contact.rb:9: Syntax error,expecting tCOLON2 or '[' or '.' ...email => a_email,:comments => a_comments,} ^

有任何想法吗?我不知道我在这里做错了什么

解决方法

添加括号正文({…})来明确指出您正在使用hash参数进行方法调用.

否则,{…}可能与身体方法中传递的函数(或lambda,无论是官方术语)混淆:body {puts“Hello world!” }

猜你在找的Ruby相关文章