Mike Andrianov
Mike Andrianov

Reputation: 3085

Rails check_box inside fields_for

I have a nested form

- f.object.buttons.build if f.object.buttons.blank?
- f.object.buttons.each do |button|
  = f.fields_for :buttons, button, index: nil do |button|
    .control-group
      = button.label :url, 'Url'
      = button.text_field :url, {type: 'url'}

    .control-group
      = button.label :active, 'Active'
      = button.check_box :active

    # ...

When I check check_box and save this form, something weird happened: in the params hash I can see, that instead of one button with checked params 'active' form sends two buttons:

..., "buttons"=>[{"label"=>"site", "url"=>"http://example.com", "active"=>"0"}, {"active"=>"1"}]}

Upvotes: 0

Views: 395

Answers (1)

MaximusDominus
MaximusDominus

Reputation: 2107

you have two variables named button in your fields_for scope

Upvotes: 1

Related Questions