【问题标题】:Undefined method true for (class) mail_form gem(类)mail_form gem 的未定义方法为真
【发布时间】:2015-12-23 08:53:45
【问题描述】:

我在 Ruby on Rails 编程方面相对较新,在尝试使用 mail_form gem 时出现此错误。 The error is in the create function as can be seen

我认为我的所有设置都是正确的:

我的模型是这样的:

class Contact < MailForm::Base
      attribute :name,        :validate => :true
      attribute :email,       :validate => /\A([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})\z/i
      attribute :message,     :validate => :true
      attribute :nickname,    :captcha => :true

    def headers
    {
      :subject => "Contact Form",
      :to => "(my email)",
      :from => %("#{name}" <#{email}>)
    }
   end
end

我不知道我试图创建一个只有一个页面的网站是否重要,所以我有在页面控制器的 welcome.html.erb 中创建新联系人的表单,但我正在定义联系人:

def welcome
   @admin = Admin.find(1)
   @projects = Project.all.order('created_at DESC')
   @contact = Contact.new
end

我也认为这很重要,创建函数(即引发错误)在联系人控制器中,我不知道是否可以,或者在与 Contact.new 相同的控制器中会更好(在页面的欢迎功能中):

def create
    @contact = Contact.new(params[:contact])
    @contact.request = request
    if @contact.deliver
      redirect_to(root_path, :notice => "Thank you for contacting me. I will reply shortly!")
    else
      flash.now[:error] = 'Cannot send message.'
    end
end

我还没有在生产中进行测试,只是在开发中可能不会在生产中发生,但我想知道为什么会发生这种情况,因为我正在关注 Mackenzie Child 的 tutorial 而这并没有不会发生在他身上,我们有相同的代码(除了我试图只在一个视图中实现这一点)。我不知道它是否有帮助,但这就是我在 config/environments/development.rb 中的内容:

config.action_mailer.delivery_method = :letter_opener

所以在开发过程中,我使用 gem letter_opener 来显示消息而不是发送邮件,尽管这对错误没有任何影响,因为在我安装 gem 之前就出现了错误。

我搜索了很多地方和所有关于堆栈溢出的问题,但我一无所获。如果你能帮助我,那就太棒了。

谢谢。

编辑: 我已经用 heroku 在生产中进行了测试并返回相同的错误:(

【问题讨论】:

    标签: ruby-on-rails controller gem


    【解决方案1】:

    好的, 我在 gem 的 github 页面中询问,我可以解决这个问题。正是在验证 Contact 模型上的属性时,我将 :true 声明为一个对象,而它必须只是一个布尔值 true(之前没有冒号)。很高兴知道避免愚蠢的错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-08-11
      • 2016-07-02
      • 1970-01-01
      • 2016-06-24
      • 2014-09-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多