0

我的模型:

class Hotel < ActiveRecord::Base
attr_accessible :name
belongs_to :user
has_many :room_types
validates :user_id, presence: true
validates :name, presence: true, length: { in: 2..15 }
end

表格代码:

    <%= simple_form_for @hotel, :validate => true do |f| %>
    <%= f.input :name %>
    <%=  f.submit 'Create Hotel' , :class => 'btn btn-primary', :type => 'submit' %>
   <% end %>

似乎没有执行任何客户端验证。我可以在不填写任何信息的情况下提交表格。有任何想法吗?

编辑:好的,我查看了我的页面的来源,它有这个错误:

window['new_hotel'] = {"type":"SimpleForm::FormBuilder","error_class":"DEPRECATION WARNING: [SIMPLE_FORM] SimpleForm.error_class 已弃用且无效。(在 (eval):1 从 error_class 调用)","error_tag":"DEPRECATION WARNING: [SIMPLE_FORM] SimpleForm.error_tag 已弃用且无效。(在 (eval):1 处从 error_tag 调用)","wrapper_error_class":"DEPRECATION WARNING: [SIMPLE_FORM] SimpleForm。 wrapper_error_class 已弃用且无效。(从 (eval):1 的 wrapper_error_class 调用)","wrapper_tag":"DEPRECATION WARNING: [SIMPLE_FORM] SimpleForm.wrapper_tag 已弃用且无效。(从 (eval) 的 wrapper_tag 调用:1)","validators":{"hotel[name]":{"presence":{"message":"不能为空"},"length":{"messages":{"maximum":"太长(最多15个字符)"},"maximum":15}}}};
4

1 回答 1

3

您必须在文件中设置browser_validations选项config/initializers/simple_form.rb

# Tell browsers whether to use default HTML5 validations (novalidate option).
# Default is enabled.
config.browser_validations = true

PS不要忘记运行

$ rails g simple_form:install

生成初始化程序。

于 2012-09-06T19:16:03.810 回答