gpt4 book ai didi

ruby-on-rails - Rails 客户端验证不适用于简单的表单关联

转载 作者:数据小太阳 更新时间:2023-10-29 08:09:42 24 4
gpt4 key购买 nike

我有一个模型用户

class User < ActiveRecord::Base
validates :goal, :presence => true, :on => :update
belongs_to :goal, :class_name => Goal, :foreign_key => "goal_id"
...
end

在我的表格上

<%= simple_form_for @users,:remote => true, :url => registration_path(resource_name), :html => { :method => :put, :multipart => true }, :validate => true do |f| %>
<%= f.association :goal,:label => "My overall goal is clearing", :input_html => {:class => 'goals'},:include_blank => true %>

但是 rails 客户端验证不接受关联验证,它让我为目标填空。

rails 客户端验证 gem 是否适用于简单的表单关联?

我的 html 页面上有以下脚本

if (window.ClientSideValidations == undefined) window.ClientSideValidations = {};
if (window.ClientSideValidations.forms == undefined) window.ClientSideValidations.forms = {};
window.ClientSideValidations.forms['edit_user_14'] = {
"type": "SimpleForm::FormBuilder",
"error_class": "error",
"error_tag": "span",
"wrapper_error_class": "field_with_errors",
"wrapper_tag": "div",
"wrapper_class": "input",
"wrapper": "default",
"validators": {
"user[mobile_number]": {
"format": [{
"message": "should be 10 digits",
"with": /^\d{10}$/i
}]
},
"user[profile_pic]": {
"integrity": [{
"message": "translation missing: en.activerecord.errors.models.user.attributes.profile_pic.integrity"
}],
"processing": [{
"message": "translation missing: en.activerecord.errors.models.user.attributes.profile_pic.processing"
}],
"download": [{
"message": "translation missing: en.activerecord.errors.models.user.attributes.profile_pic.download"
}],
"format": [{
"message": "Wrong file format",
"with": /\.(gif|jpeg|jpg|png)$/i,
"allow_blank": true
}]
}
}
};

最佳答案

这里的关键区别是使用 validates :goal_id ... 而不是 validates :goal ...

因为这是一个关联,所以您的 ActiveRecord 模型(以及底层数据存储中)的真实属性是 goal_id。这就是 client_side_validations 默认寻找的内容。

关于ruby-on-rails - Rails 客户端验证不适用于简单的表单关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14082609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com