小编jim*_*gug的帖子

Rails HABTM 设置、模型对象和 join_table 插入控制器设置

我有以下设置。

1 个产品有多个 Product_types。许多 Product_types 有 1 种类型。根据我对文档的理解,HABTM 关系。

我的模型是

class Product < ApplicationRecord
  has_and_belongs_to_many :types
end

class Type < ApplicationRecord
  has_and_belongs_to_many :products
end
Run Code Online (Sandbox Code Playgroud)

我有一个这样的连接表迁移

class CreateJoinTableProductTypes < ActiveRecord::Migration[5.1]
  def change
    create_join_table :products, :types do |t|
      t.index :product_id
      t.index :type_id
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我已经创建了一个表单 - 希望创建正确,现在我在表单提交上发送了以下参数:

"product"=>{"name"=>"Product A", "description"=>"A cool product", "image_dir_path"=>"./",
"type"=>{"id"=>"1"}},"commit"=>"Create Product"}
Run Code Online (Sandbox Code Playgroud)

我想知道 1)提交用于在表单和控制器中创建产品的参数的最佳/rails 约定是什么?

2)我如何获得插入到连接表中的记录?

我有以下获取参数的方法

def product_params
  params.require(:product).permit(:name, :description, :image_dir_path, :type,)
end
Run Code Online (Sandbox Code Playgroud)

但即使这样我也可以在日志中看到 :type 的未经允许的参数

目前我的控制器只有:

@product = Product.new(product_params)
Run Code Online (Sandbox Code Playgroud)

我非常感谢任何有关创建该对象的 Rails 方式的建议。我已经阅读了 HABTM 的 api …

controller join model ruby-on-rails has-and-belongs-to-many

0
推荐指数
1
解决办法
4061
查看次数