Rails HABTM设置,模型对象和& join_table插入控制器设置 [英] Rails HABTM setup, model object, & join_table insertion controller setup
问题描述
我有以下设置。
1个产品具有许多product_types。
许多product_types具有1种类型。
根据我对文档的理解,这是一个HABTM关系。
1 product has many product_types. many product_types have 1 type. A HABTM relationship from my understanding of the docs.
我的模型是
class Product < ApplicationRecord
has_and_belongs_to_many :types
end
class Type < ApplicationRecord
has_and_belongs_to_many :products
end
我有一个联接表迁移
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
我已经创建了一个表单-希望正确创建,现在在表单提交中发送了以下参数:
I have created a form - hopefully created correctly where now I have the following parameters being sent on form submit:
"product"=>{"name"=>"Product A", "description"=>"A cool product", "image_dir_path"=>"./",
"type"=>{"id"=>"1"}},"commit"=>"Create Product"}
我想知道
1)在表单和控制器中提交用于创建产品的参数的最佳/最佳惯例是什么?
I'm wondering 1) what is the best/rails convention for submitting the parameters for creating the product in the form and controller?
和
2)如何/如何获得插入到联接表中的记录? p>
我有以下获取参数的方法
2) how are/do I get, the records inserted into the join table?
I have the following method for getting the params
,但即使那样,在我的控制器仅有的时候,我仍然可以在日志中看到:type
def product_params
params.require(:product).permit(:name, :description, :image_dir_path, :type,)
end
的参数。
at the moment my controller only has:
我非常感谢有关创建此对象的方法的任何建议。我已经阅读了HABTM的api文档,但是当涉及到模型对象或我应该如何在控制器中处理这些东西时,什么也没看到。
@product = Product.new(product_params)
谢谢!
推荐答案
在max的评论之后,我意识到Type关系不是HABTM,而是与产品的一对多。我的HABTM产品中有另一个模型尺寸。
我的模型如下:
My Models look like this:
联接表为:
Join table is:
尺寸表为默认值
产品表为
class CreateJoinTableProductSize < ActiveRecord::Migration[5.1]
def change
create_join_table :cakes, :sizes do |t|
t.index [:product_id, :size_id]
end
end
end
Size table is default
Product table is
我的控制器是默认设置-我将以下参数列入了白名单
class CreateProducts < ActiveRecord::Migration[5.1]
def change
create_table :products do |t|
t.string :name
t.string :description
t.references :product_type, foreign_key: true
t.timestamps
end
end
end
def cake_params
params.require(:product).permit(:name,:description,:product_type_id,{:size_ids => []})
end
My controller is the default - I just have the following params whitelisted
我的_form具有以下内容
def cake_params params.require(:product).permit(:name, :description, :product_type_id, {:size_ids=>[]}) end
My _form has the following
<%= form.collection_select(:product_type_id,ProductType.all,:id,:name)%>
< / div>
< div class = field>
<%= form.label:product_size%>
<%= collection_check_boxes(:product,:size_ids,Size.all,:id,:name)%>
< / div>
<div class="field">
<%= form.label :product_type_id %>
<%= form.collection_select(:product_type_id, ProductType.all, :id, :name) %>
</div>
<div class="field">
<%= form.label :product_size %>
<%= collection_check_boxes(:product, :size_ids, Size.all, :id, :name) %>
</div>
现在我可以提交表单,对其进行编辑,所有值都将正确显示!
Now I can submit the form, edit it and all values are displayed correctly!
这篇关于Rails HABTM设置,模型对象和& join_table插入控制器设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!