问题描述
我是红宝石的新手,在以下情况下遇到麻烦。我正在尝试在帐单和项目之间建立关系。就我而言,我想在运行时生成一个帐单,例如当用户单击“创建新帐单”时,他被定向到诸如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