title: Rails nested form with cocoon. Attributes using model
body: <p>I am trying to create a nested form which has options and suboptions, both from the same model called Option. Here is the content of the files:</p>
  
  <p>Model:</p>
  
  <pre><code>class Option &lt; ApplicationRecord
    belongs_to :activity
    has_many :option_students
    has_many :students, through: :option_students
  
    has_many :suboptions,
      class_name: "Option",
      foreign_key: "option_id"
  
    belongs_to :parent,
      class_name: "Option",
      optional: true,
      foreign_key: "option_id"
  
    accepts_nested_attributes_for :suboptions,
  
      reject_if: -&gt;(attrs) { attrs['name'].blank? }
  
    validates :name, presence: true
  end
  </code></pre>
  
  <p>Controller:</p>
  
  <pre><code>class OptionsController &lt; ApplicationController
    include StrongParamsHolder
  
    def index
      @options = Option.where(option_id: nil)
    end
  
    def show
      @option = Option.find(params[:id])
    end
  
    def new
      @option = Option.new()
      1.times { @option.suboptions.build}
    end
  
    def create
      @option = Option.new(option_params)
      if @option.save
        redirect_to options_path
      else
        render :new
      end
    end
  
    def edit
      @option = Option.find(params[:id])
    end
  
    def update
      @option = Option.find(params[:id])
      if @option.update_attributes(option_params)
        redirect_to options_path(@option.id)
      else
        render :edit
      end
    end
  
    def destroy
      @option = Option.find(params[:id])
      @option.destroy
      redirect_to options_path
    end
  
  end
  </code></pre>
  
  <p>_form.html.erb:</p>
  
  <pre><code>&lt;%= form_for @option do |f| %&gt;
  
    &lt;p&gt;
      &lt;%= f.label :name %&gt;&lt;br&gt;
      &lt;%= f.text_field :name %&gt;&lt;br&gt;
      &lt;%= f.label :activity %&gt;&lt;br&gt;
      &lt;%= select_tag "option[activity_id]", options_for_select(activity_array) %&gt;&lt;br&gt;
    &lt;/p&gt;
  
    &lt;div&gt;
      &lt;div id="suboptions"&gt;
        &lt;%= f.fields_for :suboptions do |suboption| %&gt;
          &lt;%= render 'suboption_fields', f: suboption %&gt;
        &lt;% end %&gt;
  
        &lt;div class="links"&gt;
          &lt;%= link_to_add_association 'add suboption', f, :suboptions %&gt;
        &lt;/div&gt;
      &lt;/div&gt;
    &lt;/div&gt;
  
    &lt;p&gt;
      &lt;%= f.submit "Send" %&gt;
    &lt;/p&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p>_suboption_fields.html.erb</p>
  
  <pre><code>&lt;div class="nested-fields"&gt;
    &lt;%= f.label :suboption %&gt;&lt;br&gt;
    &lt;%= f.text_field :name %&gt;
    &lt;%= link_to_remove_association "X", f %&gt;
  &lt;/div&gt;
  </code></pre>
  
  <p>StrongParamsHolder:</p>
  
  <pre><code>def option_params
      params.require(:option).permit(:name, :activity_id, :students_ids =&gt; [], suboptions_attributes: [:id, :name])
  end
  </code></pre>
  
  <p>The view is created correctly, but it is not saving. It goes to "render :new" on create controller. I think it should be a problem with the params, but I am not sure what.</p>