HABTM关系中不允许的参数

时间:2019-02-09 11:47:15

标签: 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: 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

product.rb:

class Product < ApplicationRecord
  has_and_belongs_to_many :colors
end

_form.html.erb:

<div class="field">
  <%= form.label :color %>
  <%= form.collection_select  :color_ids, Color.all, :id, :color, {:multiple => true}, class: "input-field-data" %>
</div>

product_controller.rb

def product_params
  params.require(:product).permit(:title, :price, color_ids: [])
end

将参数哈希从颜色ID更改为color_ids:[:id, :color] 没什么。

此示例显然只是我重新创建的示例,目的是查看我是否在原始应用程序中做了其他错误,可能也更容易调试。

任何想法该设置有什么问题吗?我实际上有另一个项目具有完全相同的设置,并且可以正常工作吗?因此,我认为我丢失了一些东西,但是我的代码实际上没有发现任何错误。

感谢您的任何输入!

编辑:

根据要求,这是提交带有collection_select的新产品时的终端日志,如上所示:

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 console提交可以正常工作,所以这肯定与我猜想的形式有关:

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(由脚手架生成,并通过collection_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 %>

问候!

1 个答案:

答案 0 :(得分:1)

您期望使用数组/哈希,但是您收到一个字符串。看起来助手没有在创建多选,您是否在select标签上看到multiple="true" attr?如果没有,请尝试将方法更改为此:

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

注意多余的{}。助手希望第一个哈希是该助手的选项,第二个哈希是该标记的选项。