Onichan Onichan - 1 year ago 54
Ruby Question

Modify Nested_Field before adding association Cocoon

After the user submits the form, but before the association is added, is it possible to modify the nested field during this time?

For example my nested fields may look like:

<div class='nested-fields'>
<div class="field">
<%= f.label :count %>
<%= f.number_field :count %>
<%= link_to_remove_association "remove section", f %>

Say I want to multiply count x 2 before adding the association (after user submits form). Is this possible?

Or a more complex example, I might want to convert the integer to a string then save the association.

link to issue: https://github.com/nathanvda/cocoon/issues/361 (i was told to post on SO)

Answer Source

Yes, you can modify you permit params:

def some_params
  params.require(:some).permit(:count).tap do |white_list|
    white_list[:count] = 2 * params[:some][:count].to_i