1

I have two models - Campaigns & Cities (belongs to Campaign). On the create and edit form for Campaign I'm using fields_for to allow users to edit cities directly on the campaign edit page. I want to add a checkbox for each city, so that their ids will be sent in an array (delete_cities) in the params.

My attempt at it:

<%= f.fields_for :cities do | city_form | %>
      <%= city_form.label :name %>
      <%= city_form.text_field :name%>
      <%= city_form.label :phone_number %>
      <%= city_form.text_field :phone_number %>
      <%= city_form.label :zip_code %>
      <%= city_form.text_field :zip_code %>
      <%= city_form.check_box :delete_cities %>
      </br>
    <% end %>

I cant solve this any help is appeciated.

1
  • try using check_box_tag 'delete_cities[]' Commented Mar 27, 2018 at 23:23

1 Answer 1

2

According to the docs:

:allow_destroy

If true, destroys any members from the attributes hash with a _destroy key and a value that evaluates to true (eg. 1, '1', true, or 'true'). This option is off by default.

In order to allow users to delete cities, you can do like this:

<%= city_form.check_box :_destroy %>

And don't forget to permit :_destroy in the controller:

params.require(:campaign).permit(cities_attributes: [:id, :_destroy])
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.