Ruby on rails 来自控制器的Rails验证

Ruby on rails 来自控制器的Rails验证,ruby-on-rails,validation,model,controller,Ruby On Rails,Validation,Model,Controller,有一个联系人页面,可以输入姓名、电话、电子邮件和消息,然后发送给管理员的电子邮件。没有理由将消息存储在数据库中 问题。如何: 在控制器中使用Rails验证,而不使用模型,或者 在模型中使用验证,但不使用任何数据库关系 UPD: 型号: class ContactPageMessage include ActiveModel::Validations include ActiveModel::Conversion extend ActiveModel::Naming attr_accessor

有一个联系人页面,可以输入姓名、电话、电子邮件和消息,然后发送给管理员的电子邮件。没有理由将消息存储在数据库中

问题。如何:

  • 在控制器中使用Rails验证,而不使用模型,或者

  • 在模型中使用验证,但不使用任何数据库关系

  • UPD:

    型号:

    class ContactPageMessage
    include ActiveModel::Validations
    include ActiveModel::Conversion
    extend ActiveModel::Naming
    
    attr_accessor :name, :telephone, :email, :message
    validates :name, :telephone, :email, :message, presence: true
    validates :email, email_format: { :message => "Неверный формат E-mail адреса"}
    
    def initialize(attributes = {})
        attributes.each do |name, value|
          send("#{name}=", value)
        end
    end
    
    def persisted?
      false
    end
    end
    
    控制器:

    def sendmessage
    cpm = ContactPageMessage.new()
    if cpm.valid?
        @settings = Setting.first
        if !@settings
            redirect_to contacts_path, :alert => "Fail"
        end
        if ContactPageMessage.received(params).deliver
            redirect_to contacts_path, :notice => "Success"
        else
            redirect_to contacts_path, :alert => "Fail"
        end
    else
        redirect_to contacts_path, :alert => "Fail"
    end
    end
    end
    

    我仍然建议您使用模型,rails模型不必继承自
    ActiveRecord::Base
    。 例如:

    class Contact
      include ActiveModel::Validations
      attr_accessor :name, :telephone, :email, :message
      validates_presence_of :name, :telephone, :email, :message
      validates_format_of :email, with: EMAIL_REGEXP
    end
    
    您可以在控制器中使用它:

    contact = Contact.new
    # ...
    if contact.valid?
      # do something
    else
      # do something else
    end
    

    在您的模型中,您可以添加下面的方法,它将只为消息设置getter和setter方法,并且您可以对消息进行验证,而无需在数据库中设置列

    attr_accessor :message
    validates :message, presence: true
    

    您应该使用模型,而不从
    ActiveRecord::Base
    类继承

    class ContactPageMessage
    
      include ActiveModel::Validations
      include ActiveModel::Conversion
      extend ActiveModel::Naming
    
      attr_accessor :whatever
    
      validates :whatever, :presence => true
    
      def initialize(attributes = {})
        attributes.each do |name, value|
          send("#{name}=", value)
        end
      end
    
      def persisted?
        false
      end
    
    end
    
    通过此操作,您将能够初始化新对象,并能够对该对象调用验证

    我认为您有一个同名的不同类名,在控制器代码中,我可以看到:

    if ContactPageMessage.received(params).deliver
        redirect_to contacts_path, :notice => "Success"
    else
    
    如果这是您的邮件类,请将其名称更改为
    ContactPageMessageMailer
    。你不会犯那个错误的


    希望这会有帮助。谢谢

    我为ContactPageMessage:Class`(使用Rails 4,可能有错误?)调用了
    私有方法“
    new`(使用Rails 4),请将应用程序跟踪粘贴到此处。您如何调用新方法?如果cpm.valid,请在此处写入。def sendmessage cpm=ContactPageMessage.new()?@settings=Setting。首先…@Rom您能把它粘贴到您的问题中吗?它将更具可读性。
    class Contact
    验证:message,:presence=>true
    end
    #=>NoMethodError:undefined method
    验证Contact:class`如果您已经有一个模型,并且只有很少的属性不需要存储在数据库中,那么您可以使用此选项。上面的选项只能包含在从ActiveRecord::Base继承的模型上。如果您想要一个独立的类,它应该使用Rails4充当模型,这非常简单,您只需要在类中包含ActiveModel::Model并执行上述操作。我想你已经有了一个同名的类“ContactPageMessage”。这就是问题所在。你想在数据库中保存姓名、电话和电子邮件,并且只需要验证消息而不保存在数据库中吗?@sumi,我不想将任何内容保存到数据库中。刚刚验证。我已经更新了我的答案。@RailsGuy,宾果!真丢脸,我用的是同一个邮件类。多谢各位!