Ruby on rails HABTM关系中不允许的参数

Ruby on rails HABTM关系中不允许的参数,ruby-on-rails,Ruby On Rails,因此,我正在建立HABTM关系,当我提交表单时,我总是在终端中返回此错误: Unpermitted parameter: :color_ids 没有其他错误。该应用程序运行良好,只是关联始终为空数组 Schema.rb: create_table "colors", force: :cascade do |t| t.string "color" t.datetime "created_at", null: false t.datetime "updated_at", null: f

因此,我正在建立HABTM关系,当我提交表单时,我总是在终端中返回此错误:

Unpermitted parameter: :color_ids
没有其他错误。该应用程序运行良好,只是关联始终为空数组

Schema.rb:

create_table "colors", force: :cascade do |t|
  t.string "color"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end

create_table "colors_products", id: false, force: :cascade do |t|
  t.integer "color_id", null: false
  t.integer "product_id", null: false
end

create_table "products", force: :cascade do |t|
  t.string "title"
  t.decimal "price"
  t.datetime "created_at", null: false
  t.datetime "updated_at", null: false
end
color.rb:

class Color < ApplicationRecord
  has_and_belongs_to_many :products
end
将参数哈希从color id更改为color\u id:[:id,:color] 没什么区别

很明显,这个示例是我重新创建的,目的是查看我在原始应用程序中是否犯了其他错误,可能也更容易调试

你知道那个装置有什么问题吗?我实际上有另一个项目与完全相同的设置,它的工作?正因为如此,我认为我遗漏了一些东西,但实际上我并没有发现我的代码有任何错误

提前感谢您的任何意见

编辑:

根据要求,以下是提交新产品和集合时的终端日志,如上图所示:

Started POST "/products" for 127.0.0.1 at 2019-02-10 14:02:59 +0100
Processing by ProductsController#create as HTML
  Parameters: {"authenticity_token"=>"+f+GJaN58M029eGICvMqlwtjYB4Qmv/KNBY0OnymrxyFy+zNYXKfZtCXR0NM3kLY16QIzfLb+takhNjgIQXeEw==", "product"=>{"title"=>"abc", "price"=>"9.99", "color_ids"=>"1"}, "commit"=>"Create Product"}
Unpermitted parameter: :color_ids
   (0.1ms)  begin transaction
  ↳ app/controllers/products_controller.rb:30:in `block in create'
  Product Create (1.0ms)  INSERT INTO "products" ("title", "price", "created_at", "updated_at") VALUES (?, ?, ?, ?)  [["title", "abc"], ["price", 9.99], ["created_at", "2019-02-10 13:02:59.634965"], ["updated_at", "2019-02-10 13:02:59.634965"]]
  ↳ app/controllers/products_controller.rb:30:in `block in create'
   (1.1ms)  commit transaction
  ↳ app/controllers/products_controller.rb:30:in `block in create'
Redirected to http://localhost:3000/products/15
Completed 302 Found in 14ms (ActiveRecord: 2.3ms | Allocations: 3885)


Started GET "/products/15" for 127.0.0.1 at 2019-02-10 14:02:59 +0100
Processing by ProductsController#show as HTML
  Parameters: {"id"=>"15"}
  Product Load (0.4ms)  SELECT "products".* FROM "products" WHERE "products"."id" = ? LIMIT ?  [["id", 15], ["LIMIT", 1]]
  ↳ app/controllers/products_controller.rb:67:in `set_product'
  Rendering products/show.html.erb within layouts/application
  Rendered products/show.html.erb within layouts/application (Duration: 1.1ms | Allocations: 302)
Completed 200 OK in 23ms (Views: 16.0ms | ActiveRecord: 0.4ms | Allocations: 8945)
此外:

通过rails控制台提交效果很好,所以我想这肯定与表单有关:

irb(main):010:0> p = Product.last

=> #<Product id: 15, title: "abc", price: 0.999e1, created_at: "2019-02-10 13:02:59", updated_at: "2019-02-10 13:02:59">

irb(main):011:0> p.colors

=> #<ActiveRecord::Associations::CollectionProxy []>

irb(main):012:0> p.colors << [Color.last]

=> #<ActiveRecord::Associations::CollectionProxy [#<Color id: 2, col: "Red", created_at: "2019-02-10 09:04:42", updated_at: "2019-02-10 09:04:42">]>

irb(main):013:0> p.colors

=> #<ActiveRecord::Associations::CollectionProxy [#<Color id: 2, col: "Red", created_at: "2019-02-10 09:04:42", updated_at: "2019-02-10 09:04:42">]>
_form.html.erb由scaffold生成,并使用collection\u select字段进行调整

<%= form_with(model: product, local: true) do |form| %>
  <% if product.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2>

      <ul>
      <% product.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :title %>
    <%= form.text_field :title %>
  </div>

  <div class="field">
    <%= form.label :price %>
    <%= form.text_field :price %>
  </div>

  <div class="field">
    <%= form.label :color_ids %>
    <%= form.collection_select( :color_ids,  Color.all, :id, :col, {multiple: true}) %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>

你好

您需要一个数组/散列,但收到的是一个字符串。看起来助手没有创建多重选择,您是否在选择标记上看到multiple=true属性?如果不是,请尝试将方法更改为:

form.collection_select( :color_ids,  Color.all, :id, :col, {}, {multiple: true})

注意额外的{}。帮助程序希望第一个散列是帮助程序的选项,第二个散列是标记的选项。

您是否尝试过在product.rb中使用accepts\u nested\u attributes\u for:colors?可能会有帮助。你可以在请求时显示服务器的日志吗?@arieljuod当然,我已经用日志更新了问题。我唯一的问题是:添加多个时会返回错误:false或完全删除多个。知道为什么会这样吗?color_id setter需要一个id数组,如果从select中删除multiple=true属性,那么只会收到一个颜色id作为字符串,而不是数组。我不明白如果你不使用多重选择,为什么你会有一个HABTM关系,尽管它更可能是一个属于关系。如果你能详细解释一下为什么你不想选择多个,也许我可以建议一些更好的。好吧,我们的想法是在以后为每种颜色添加一个数量栏,这样一件产品例如t恤就可以有多个不同数量的颜色。想想一个店主,他想增加他的存货,他目前有3件白衬衫和2件蓝衬衫。在这种情况下,一个产品有许多颜色和颜色,许多产品。但由于数量的原因,每种颜色也必须分开。因此,我最初的想法是将多个集合选择添加到表单中,以便店主可以单独添加多个颜色,这将使多个true变得不必要。如果需要为关联添加额外字段,则需要使用has_mu many:through关联和联接模型来保存ID和数量检查指南。常用的方法是在带有activerecords“accept\u nested\u attributes\u”的表单上使用嵌套表单字段作为方法。我知道:通过关联,但不知道在这种情况下我需要它们。我去看看!谢谢你的帮助!
irb(main):010:0> p = Product.last

=> #<Product id: 15, title: "abc", price: 0.999e1, created_at: "2019-02-10 13:02:59", updated_at: "2019-02-10 13:02:59">

irb(main):011:0> p.colors

=> #<ActiveRecord::Associations::CollectionProxy []>

irb(main):012:0> p.colors << [Color.last]

=> #<ActiveRecord::Associations::CollectionProxy [#<Color id: 2, col: "Red", created_at: "2019-02-10 09:04:42", updated_at: "2019-02-10 09:04:42">]>

irb(main):013:0> p.colors

=> #<ActiveRecord::Associations::CollectionProxy [#<Color id: 2, col: "Red", created_at: "2019-02-10 09:04:42", updated_at: "2019-02-10 09:04:42">]>
<%= form_with(model: product, local: true) do |form| %>
  <% if product.errors.any? %>
    <div id="error_explanation">
      <h2><%= pluralize(product.errors.count, "error") %> prohibited this product from being saved:</h2>

      <ul>
      <% product.errors.full_messages.each do |message| %>
        <li><%= message %></li>
      <% end %>
      </ul>
    </div>
  <% end %>

  <div class="field">
    <%= form.label :title %>
    <%= form.text_field :title %>
  </div>

  <div class="field">
    <%= form.label :price %>
    <%= form.text_field :price %>
  </div>

  <div class="field">
    <%= form.label :color_ids %>
    <%= form.collection_select( :color_ids,  Color.all, :id, :col, {multiple: true}) %>
  </div>

  <div class="actions">
    <%= form.submit %>
  </div>
<% end %>
form.collection_select( :color_ids,  Color.all, :id, :col, {}, {multiple: true})