【问题标题】:Nested forms in Ruby with checkboxes and many-to-many relationshipRuby 中带有复选框和多对多关系的嵌套表单
【发布时间】:2023-04-01 08:54:01
【问题描述】:

我是 ruby​​ 新手,在以下情况下遇到问题。我正在尝试在账单和项目之间建立关系。在我的例子中,我想在运行时生成一个账单,比如当用户点击创建新账单时,他被定向到 http://localhost:3000/bills/new 之类的路由,然后他有一个项目列表,其中他必须通过选中复选框并添加数量来进行选择。我有 3 张桌子,物品,账单,BillItems。他们有以下字段:

    create_table "bill_items", force: :cascade do |t|
    t.integer "bill_id"
    t.integer "item_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "quantity"
  end

  create_table "bills", force: :cascade do |t|
    t.integer "user_id"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
  end

    create_table "items", force: :cascade do |t|
    t.string "name"
    t.float "price"
    t.datetime "created_at", precision: 6, null: false
    t.datetime "updated_at", precision: 6, null: false
    t.integer "category_id"
  end  

我的模型是这样创建的:

比尔.rb

 class Bill < ApplicationRecord
        has_many :bill_items
        has_many :items, through: :bill_items
        accepts_nested_attributes_for :bill_items, :allow_destroy => true, :reject_if => :all_blank
    end

项目.rb

class Item < ApplicationRecord
    
    has_many :bill_items
    has_many :bills, through: :bill_items

end

BillItem.rb

class BillItem < ApplicationRecord
    belongs_to :bill
    belongs_to :item
    
end

我的表格如下:

<%= form_for @bill do |f| %>
 <% if @allItems %>

     <% @allItems.each_with_index do |item, index| %>
         <tr class="table-success" scope="col-8">
        <%= f.fields_for :bill_items do |s| %>
            <td class="text-secondary"><%= item.category.name %></td>
            <%= s.hidden_field :name, value: item.name %>
            <td class="text-primary"><%= s.label item.name %></td>
            <td><%= check_box_tag "item_ids[]", item.id, false, class: 'selectable' %> </td> 
            <td><%= s.number_field(:quantity, in: 1.0..100.0, step: 1) %></td>
            <td><%= s.label  :price, item.price %></td>
           
        <% end %>
        </tr>
     <% end %>
 <% end %>
    </tbody>
</table>
   <div class="form-group row justify-content-center">
    <%= f.submit "Create Order with Selected items", class: "btn btn-secondary" %>
    </div>
<% end %>

然后我的控制器设置如下:

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

当我运行我的代码并将数据发送到表单并通过 byebug 检查参数时,它会显示我遵循的参数,而我选择了 ids 1 和 4 的两项:

<ActionController::Parameters {"authenticity_token"=>"hVnrTkWxWwuXqS4tb01INVkNwRaFooVERKe2L8YkXykyPqImKCVRrvqjhK8sA0Q26nsOS+dSNdLvIOPTfis8nQ==", "bill"=>{"bill_items_attributes"=>{"0"=>{"name"=>"sheer", "quantity"=>"2"}, "1"=>{"name"=>"burger", "quantity"=>""}, "2"=>{"name"=>"custurs", "quantity"=>""}, "3"=>{"name"=>"sib", "quantity"=>"4"}}}, "item_ids"=>["1", "4"], "commit"=>"Create Order with Selected items", "controller"=>"bills", "action"=>"create"} permitted: false>

然后我点击提交,它只在数据库中保存账单并给我错误

Unpermitted parameter: :name
Unpermitted parameter: :name
Unpermitted parameter: :name
Unpermitted parameter: :name`

我尝试了很多技术,但找不到解决方案。如果有人可以帮助我,这将非常有帮助。即使我需要重新设计我的逻辑,也要帮我解决这个问题。谢谢。

【问题讨论】:

    标签: ruby-on-rails ruby rails-activerecord strong-parameters


    【解决方案1】:
    <td><%= check_box_tag "item_ids[]", item.id, false, class: 'selectable' %> </td>
    

    必须是

    <td><%= check_box_tag "bill[item_ids[]]", item.id, false, class: 'selectable' %> </td>
    

    【讨论】:

    • 请让我知道需要在我的控制器参数或模式中进行的任何其他更改。谢谢
    【解决方案2】:

    问题:嵌套属性命名不匹配

    Bill 模型正在接受 :items 的嵌套属性 在控制器上,您已指定 :bill_items_attributes 和 bill_items 的表单生成字段 - f.fields_for :bill_items

    解决方案:保持一致

    关于比尔模型 -

    accepts_nested_attributes_for :items

    在账单控制器上 -

    permit(items_attributes:

    在 new.html.erb 表单上 -

    f.fields_for :items

    但是,这会给 items_id 带来另一个问题,我不确定要在那里实现什么。

    【讨论】:

      【解决方案3】:

      您收到这些错误是因为您在表单中有 hidden_​​field :name,提交表单时不需要。我删除了那个。但是,强参数和复选框命名存在问题。我更正了强参数并保持复选框命名描述性,以便您可以看到表单将如何生成它。

      试试这个代码。我能够使用此代码创建记录。

      bills_controller.rb

      private 
          def bill_params
               params.require(:bill).permit(bill_items_attributes: [:quantity, :item_id])
          end
      

      new.html.erb

      <%= form_for @bill do |f| %>
          <% if @allItems %>
              <% @allItems.each_with_index do |item, index| %>
                  <%= f.fields_for :bill_items do |s| %>
                      <tr class="table-success" scope="col-8">
                          <td class="text-primary"><%= s.label item.name %></td>
                          <td><%= check_box_tag "bill[bill_items_attributes][#{index}][item_id]", item.id, false, class: 'selectable' %> </td> 
                          <td><%= s.number_field(:quantity, in: 1.0..100.0, step: 1) %></td>
                          <td><%= s.label  :price, item.price %></td>
                      </tr>
                  <% end %>
              <% end %>
          <% end %>
         
          <div class="form-group row justify-content-center">
              <%= f.submit "Create Order with Selected items", class: "btn btn-secondary" %>
          </div>
      <% end %>
      

      【讨论】:

        猜你喜欢
        • 2012-10-14
        • 1970-01-01
        • 2011-05-27
        • 1970-01-01
        • 2013-04-29
        • 1970-01-01
        • 2018-10-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多