[英]Rails 4 Active Model Validations
我正在尝试使用Active Model在我的网站上创建“联系我们”表单。
我遇到的问题是错误消息永远不会返回。
我正在使用远程表单。
路线:
resource :front_contacts, only: :create, controller: :front_contact
控制器:
class FrontContactController < ApplicationController
def create
contact = FrontContact.new(params[:front_contact])
@errors = contact.errors.size
end
end
front_contact:
class FrontContact
include ActiveModel::Model
attr_accessor :name, :email, :message
validates_presence_of :name, :message
validates_format_of :email, with: /[a-zA-Z0-9._%-]+@(?:[a-zA-Z0-9-]+\.)+(com|net|org|info|biz|me|edu|gov)/i
end
js.erb:
alert(<%= @errors %>);
警报始终警报为零。
请指教。
如果您使用的是Rails 4,则新添加了一些强大的参数,这些参数可能会阻止您创建模型。
您的控制器中是否有类似以下内容的内容?
params.require(:front_contact).permit!
最初切换到Rails 4时,我遇到了与您相同的问题,并且感到困惑,因为不允许使用特定的参数不会引发错误消息。 只是没有创建对象。
如果你有一个RailsCasts帐户,有关于如何应对强大的PARAMS一个真正伟大的视频在这里 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.