Reject creation of nested attribute if checkbox checked
<p>I have a form in my rails app that accepts nested attributes. However, what I want to do is for rails to reject the creation of the nested model if a checkbox (outside the model itself) is checked.</p>

<p>Any idea on how to pass an attribute to the :reject_if option of the accepts_nested_attributes_for in the model from the controller?</p>

<p>Thank you very much in advance.</p>

<p><strong>EDIT:</strong></p>

<p>My controller looks like this:</p>

<pre><code>def new
  @course = Course.new
  @course.course_template = CourseTemplate.new
end

def create
  @course = Course.new(course_params)
  @course.user = current_user
  if @course.save
    flash[:success] = t(".new_course_created_succefully")
    redirect_to courses_path
  else
    render 'new'
  end
end
</code></pre>

<p>And the form:</p>

<pre><code>&lt;%= form_for @course do |f| %&gt;
  &lt;%= render 'shared/error_messages', error_model: @course %&gt;

  &lt;div class="form-group has-feedback mb"&gt;
    &lt;%= f.label :name %&gt;
    &lt;%= f.text_field :name, class: 'form-control' %&gt;
  &lt;/div&gt;

  &lt;div class="form-group has-feedback mb"&gt;
    &lt;div class="checkbox c-checkbox needsclick"&gt;
      &lt;label class="needsclick"&gt;
        &lt;%= check_box_tag "template", "1", false, {class: "needsclick"} %&gt;
        &lt;span class="fa fa-check"&gt;&lt;/span&gt;Is Template?
      &lt;/label&gt;
    &lt;/div&gt;
  &lt;/div&gt;

  &lt;%= f.fields_for :course_template do |ff| %&gt;
    &lt;div class="form-group has-feedback mb"&gt;
      &lt;%= ff.label :name %&gt;
      &lt;%= ff.text_field :name %&gt;
    &lt;/div&gt;
  &lt;% end %&gt;
&lt;% end %&gt;
</code></pre>