【发布时间】: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