Aydar Omurbekov
Aydar Omurbekov

Reputation: 2117

How to add 'selected' attribute for nested_form select

I am using this NestedForm gem for dynamically adding form fields,
but when adding a selectbox field it doesn't have the "selected" attribute,

<%= nested_form_for [@website, @website_rule] do |f| %>
 <%= f.fields_for :website_dynamic_rules do |rule_form| %>
                <%= rule_form.select :all_pages, [['Yes', true], ['No', false]],{}, { :class => 'all-pages'%>
<% end %>
<% end %>

Upvotes: 0

Views: 76

Answers (1)

cschroed
cschroed

Reputation: 6834

If you are not seeing the selected attribute in one of the option elements, my guess would be that you have a value of nil in the all_pages attribute rather than a value of false. To see if this is the case you can temporarily add something like this in your view:

<%= f.fields_for :website_dynamic_rules do |rule_form| %>
  <%= rule_form.object.inspect %>

That will show you the object that is being used and you can review the value for all_pages. If it is nil, one possible solution would be to define a default value in the migration. When you first add the column, you would want to use something like:

add_column :website_rules, :all_pages, :boolean, default: false

Upvotes: 1

Related Questions