title: Save checkbox value in database in rails
body: <p>I have a form like this</p>
  
  <pre><code> &lt;div class="field"&gt;
      &lt;%= f.label :name %&gt;&lt;br /&gt;
      &lt;%= f.text_field :name %&gt;
    &lt;/div&gt;
    &lt;div class="field"&gt;
      &lt;% counter = 1 %&gt;
      &lt;% @semester.each do |semester|  %&gt;
          &lt;% checkboxs = "subchecked" + counter.to_s %&gt;
          &lt;%= check_box_tag(checkboxs, semester.id) %&gt;
          &lt;%= label_tag(checkboxs, semester.name) %&gt;
          &lt;% counter = counter + 1 %&gt;
      &lt;% end %&gt;
    &lt;/div&gt;
  </code></pre>
  
  <p>and In my create action I tried something like this</p>
  
  <pre><code>def create
      @subject = Subject.new(params[:subject])
  
           sem =  params[:checkboxs]
      name = params[:name]       
      counter = 0
  
      respond_to do |format|
        if @subject.save
  
          sem.each do |sem|              
            sem.update_attributes!(:name =&gt; name, :semester_id =&gt; sem[counter])
            counter = counter + 1
          end
  
          format.html { redirect_to @subject, notice: 'Subject was successfully created.' }
          format.json { render json: @subject, status: :created, location: @subject }
        else
          format.html { render action: "new" }
          format.json { render json: @subject.errors, status: :unprocessable_entity }
        end
      end
    end
  </code></pre>
  
  <p>But this doesn't have the values instead it shows error </p>
  
  <pre><code>undefined method `each' for nil:NilClass
  
  My new action
        def new
          @subject = Subject.new
          @semester = Semester.all
          respond_to do |format|
            format.html # new.html.erb
            format.json { render json: @subject }
          end
        end
  </code></pre>