Rails 动态选择 - 验证未显示字段错误

发布于 2024-12-07 08:29:27 字数 971 浏览 3 评论 0原文

在我的表单中,我有两个选择 - 州和城市。城市下拉列表取决于用户在州下拉列表中选择的内容。在我看来,我有以下内容:

<%= f.input :state_id, :collection => @states, :prompt => "Pick Your State" %>
<%= render :partial => 'cities', :locals => { :cities => @cities, :user => form } %>

城市部分如下所示:

<%= simple_fields_for :user do |fields| %>
  <% if cities.blank? %>
    <%= fields.input :city_id, :collection => cities, :prompt => "Pick Your State" %>
  <% else %>
    <%= fields.input :city_id, :collection => cities, :prompt => "Pick Your City" %>
  <% end %>
<% end %>

在我的模型中: validates_presence_of :state_id, :city_id

下拉菜单功能符合预期 - 如果重要的话,我正在使用 simple_form 和 jquery 。验证“起作用”是因为如果我不进行选择,记录将不会保存,而且实际上,我收到了解释问题的错误消息。然而,没有发生的是城市周围的 field_with_errors 包装器。我怀疑这与我使用部分有关,但我不确定。我需要创建某种自定义验证吗?

编辑:

我还注意到我的密码确认字段也没有获得 field_with_errors div (同样,尽管验证本身仍在强制执行)

In my form, I have two selects - state and city. The city drop-down depends upon what the user selects in the state drop-down. In my view, I have the following:

<%= f.input :state_id, :collection => @states, :prompt => "Pick Your State" %>
<%= render :partial => 'cities', :locals => { :cities => @cities, :user => form } %>

The city partial looks like this:

<%= simple_fields_for :user do |fields| %>
  <% if cities.blank? %>
    <%= fields.input :city_id, :collection => cities, :prompt => "Pick Your State" %>
  <% else %>
    <%= fields.input :city_id, :collection => cities, :prompt => "Pick Your City" %>
  <% end %>
<% end %>

In my model:
validates_presence_of :state_id, :city_id

The drop-downs function as expected - I'm using simple_form and jquery if that matters. The validations "work" in that if I don't make a selection, the record won't save, and indeed, I get the error messages explaining the problem. What is not happening, however, is the field_with_errors wrapper around the city. I suspect it has to do with the fact that I am using the partial, but I'm not sure. Do I need to create a custom validation of some kind?

edit:

I also notice that my password confirmation field is not getting the field_with_errors div as well (again, though the validation itself is still being enforced)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

宛菡 2024-12-14 08:29:27

我将城市选择移出部分。这是以不同的提示为代价的,但我可以忍受。希望它能帮助某人。

我想知道密码确认字段没有得到包装器。

I moved the city select out of the partial. It's at the cost of the different prompts, but I can live with it. Hope it helps someone.

Would like to know the password confirmation field doesn't get the wrapper though.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文