【问题标题】:Rails validation from controller从控制器进行 Rails 验证
【发布时间】:2013-07-31 08:45:57
【问题描述】:

有一个联系页面,提供输入姓名、电话、电子邮件和消息,然后发送到管理员的电子邮件。没有理由将消息存储在 DB 中。

问题。如何:

  1. 在控制器中使用 Rails 验证,根本不使用模型,或者

  2. 在模型中使用验证,但没有任何数据库关系

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

【问题讨论】:

  • 我想你已经有一个同名的类'ContactPageMes​​sage'。这就是问题所在。
  • 您是否要将姓名、电话和电子邮件保存在数据库中,并且只需要验证消息而不保存在数据库中?
  • @sumi,我不想将任何东西保存到数据库中。刚刚验证。
  • 我已经更新了我的答案。
  • @RailsGuy,宾果游戏!真丢人,我用过同样的邮件类。非常感谢!

标签: ruby-on-rails validation model controller


【解决方案1】:

您应该使用模型而不是从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。你不会得到那个错误的记录。

希望它会有所帮助。谢谢

【讨论】:

  • 我得到了这个:private method'new' 调用了 ContactPageMes​​sage:Class`(使用 Rails 4,可能有错误?)
  • 在此处粘贴您的应用程序跟踪。
  • 你是如何调用新方法的?写在这里。
  • def sendmessage cpm = ContactPageMes​​sage.new() if cpm.valid? @settings = Setting.first...
  • @Rom 你能把它粘贴到你的问题中吗?它将更具可读性。
【解决方案2】:

我仍然建议您使用模型,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

【讨论】:

    【解决方案3】:

    在您的模型中,您可以添加以下内容,这将为消息设置 getter 和 setter 方法,并且您可以对消息进行验证,而无需在 db 中有列

    attr_accessor :message
    validates :message, presence: true
    

    【讨论】:

    • class Contact validates :message, :presence => true end # => NoMethodError: undefined method validates' for Contact:Class`
    • 如果您已经有一个模型并且只有少数属性不需要将其存储在数据库中,那么您可以使用此选项。上述选项只能包含在继承自 ActiveRecord::Base 的模型上。如果你想要一个独立的类,它应该使用 Rails4 作为模型,这很容易,你只需要在你的类中包含 ActiveModel::Model 并执行上述操作。参考link
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-19
    • 1970-01-01
    相关资源
    最近更新 更多