title: Nested forms in Ruby with checkboxes and many-to-many relationship
body: <p>I am new to ruby and having a trouble in following scenario. I am trying to build a relationship between bill and items. In my case, I want to generate a bill at run time like when user clicks on create new bill, he is directed to a route like http://localhost:3000/bills/new and then he has a list of items from which he has to choose by checking the checkboxes and adding the quantity. I have 3 tables, Items, Bills, BillItems. They have following fields in them:</p>
  <pre><code>    create_table &quot;bill_items&quot;, force: :cascade do |t|
      t.integer &quot;bill_id&quot;
      t.integer &quot;item_id&quot;
      t.datetime &quot;created_at&quot;, precision: 6, null: false
      t.datetime &quot;updated_at&quot;, precision: 6, null: false
      t.integer &quot;quantity&quot;
    end
  
    create_table &quot;bills&quot;, force: :cascade do |t|
      t.integer &quot;user_id&quot;
      t.datetime &quot;created_at&quot;, precision: 6, null: false
      t.datetime &quot;updated_at&quot;, precision: 6, null: false
    end
  
      create_table &quot;items&quot;, force: :cascade do |t|
      t.string &quot;name&quot;
      t.float &quot;price&quot;
      t.datetime &quot;created_at&quot;, precision: 6, null: false
      t.datetime &quot;updated_at&quot;, precision: 6, null: false
      t.integer &quot;category_id&quot;
    end  
  </code></pre>
  <p>I have my models created like this:</p>
  <p>Bill.rb</p>
  <pre><code> class Bill &lt; ApplicationRecord
          has_many :bill_items
          has_many :items, through: :bill_items
          accepts_nested_attributes_for :bill_items, :allow_destroy =&gt; true, :reject_if =&gt; :all_blank
      end
  </code></pre>
  <p>Item.rb</p>
  <pre><code>class Item &lt; ApplicationRecord
      
      has_many :bill_items
      has_many :bills, through: :bill_items
  
  end
  </code></pre>
  <p>BillItem.rb</p>
  <pre><code>class BillItem &lt; ApplicationRecord
      belongs_to :bill
      belongs_to :item
      
  end
  </code></pre>
  <p>I have my form like:</p>
  <pre><code>&lt;%= form_for @bill do |f| %&gt;
   &lt;% if @allItems %&gt;
  
       &lt;% @allItems.each_with_index do |item, index| %&gt;
           &lt;tr class=&quot;table-success&quot; scope=&quot;col-8&quot;&gt;
          &lt;%= f.fields_for :bill_items do |s| %&gt;
              &lt;td class=&quot;text-secondary&quot;&gt;&lt;%= item.category.name %&gt;&lt;/td&gt;
              &lt;%= s.hidden_field :name, value: item.name %&gt;
              &lt;td class=&quot;text-primary&quot;&gt;&lt;%= s.label item.name %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= check_box_tag &quot;item_ids[]&quot;, item.id, false, class: 'selectable' %&gt; &lt;/td&gt; 
              &lt;td&gt;&lt;%= s.number_field(:quantity, in: 1.0..100.0, step: 1) %&gt;&lt;/td&gt;
              &lt;td&gt;&lt;%= s.label  :price, item.price %&gt;&lt;/td&gt;
             
          &lt;% end %&gt;
          &lt;/tr&gt;
       &lt;% end %&gt;
   &lt;% end %&gt;
      &lt;/tbody&gt;
  &lt;/table&gt;
     &lt;div class=&quot;form-group row justify-content-center&quot;&gt;
      &lt;%= f.submit &quot;Create Order with Selected items&quot;, class: &quot;btn btn-secondary&quot; %&gt;
      &lt;/div&gt;
  &lt;% end %&gt;
  </code></pre>
  <p>Then I have my controller setup like this:</p>
  <pre><code>def new
      @bill = Bill.new
      @bill_items = @bill.bill_items.build
      
  end
  
  def create 
      byebug
      @bill = Bill.new(bill_params)
      @bill.save
      redirect_to new_bill_path
  
  end
  
  private 
      def bill_params
           params.require(:bill).permit(bill_items_attributes: [:quantity, :item_ids])
      end
  </code></pre>
  <p>When I run my code and send data to form and check params via byebug it shows me following params, while i selected two items, of ids 1 and 4:</p>
  <pre><code>&lt;ActionController::Parameters {&quot;authenticity_token&quot;=&gt;&quot;hVnrTkWxWwuXqS4tb01INVkNwRaFooVERKe2L8YkXykyPqImKCVRrvqjhK8sA0Q26nsOS+dSNdLvIOPTfis8nQ==&quot;, &quot;bill&quot;=&gt;{&quot;bill_items_attributes&quot;=&gt;{&quot;0&quot;=&gt;{&quot;name&quot;=&gt;&quot;sheer&quot;, &quot;quantity&quot;=&gt;&quot;2&quot;}, &quot;1&quot;=&gt;{&quot;name&quot;=&gt;&quot;burger&quot;, &quot;quantity&quot;=&gt;&quot;&quot;}, &quot;2&quot;=&gt;{&quot;name&quot;=&gt;&quot;custurs&quot;, &quot;quantity&quot;=&gt;&quot;&quot;}, &quot;3&quot;=&gt;{&quot;name&quot;=&gt;&quot;sib&quot;, &quot;quantity&quot;=&gt;&quot;4&quot;}}}, &quot;item_ids&quot;=&gt;[&quot;1&quot;, &quot;4&quot;], &quot;commit&quot;=&gt;&quot;Create Order with Selected items&quot;, &quot;controller&quot;=&gt;&quot;bills&quot;, &quot;action&quot;=&gt;&quot;create&quot;} permitted: false&gt;
  </code></pre>
  <p>Then I click submit and it only saves bill in the db and gives me error</p>
  <pre><code>Unpermitted parameter: :name
  Unpermitted parameter: :name
  Unpermitted parameter: :name
  Unpermitted parameter: :name`
  </code></pre>
  <p>I have tried many techniques and couldn't find a solution. It will be very helpful if someone can help me with this. Even if I need to redesign my logic then do help me with this. Thanks.</p>