开发者

Rails dynamic selects - validations not showing errors on fields

开发者 https://www.devze.com 2023-04-09 19:14 出处:网络
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:

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.i开发者_StackOverflownput :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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消