Ruby中带有复选框和多对多关系的嵌套表单

如何解决Ruby中带有复选框和多对多关系的嵌套表单

我是红宝石的新手,在以下情况下遇到麻烦。我正在尝试在帐单和项目之间建立关系。就我而言,我想在运行时生成一个帐单,例如当用户单击“创建新帐单”时,他被定向到诸如http:// localhost:3000 / bills / new之类的路由,然后他具有一个项目清单,他必须通过选中复选框并添加数量来进行选择。我有3个表格,Items,Bills,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",null: false
    t.integer "quantity"
  end

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

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

我的模型是这样创建的:

Bill.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

Item.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检查参数时,它向我显示以下参数,而我选择了ID为1和4的两项:

<ActionController::Parameters {"authenticity_token"=>"hVnrTkWxWwuXqS4tb01INVkNwRaFooVERKe2L8YkXykyPqImKCVRrvqjhK8sA0Q26nsOS+dSNdLvIOPTfis8nQ==","bill"=>{"bill_items_attributes"=>{"0"=>{"name"=>"sheer","quantity"=>"2"},"1"=>{"name"=>"burger","quantity"=>""},"2"=>{"name"=>"custurs","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`

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

解决方法

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

必须是

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

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

比尔模型接受:items 的嵌套属性 在Controller上,您已指定: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带来另一个问题,我不确定该如何解决。

,

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

尝试此代码。我可以用此代码创建记录。

:name

bills_controller.rb

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

new.html.erb

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?