【发布时间】:2016-09-10 15:17:29
【问题描述】:
我有类别模型,我希望能够与不同的模型一起使用。这就是我最终将 Polymorphic 与 has_many 一起使用的原因。
使用 Rails_admin 一切正常。但是,当我想自己创建一个表单时,我似乎无法保存它。这是我所拥有的:
category.rb
class Category < ActiveRecord::Base
has_many :categorizings, inverse_of: :category, dependent: :destroy
has_many :cars, through: :categorizings, :source => :categorizable,
:source_type => 'Car'
end
分类.rb
class Categorizing < ActiveRecord::Base
belongs_to :category
belongs_to :categorizable, :polymorphic => true
end
汽车.rb
class Car < ActiveRecord::Base
has_many :categorizings, :as => :categorizable, inverse_of: :car, dependent: :destroy
has_many :categories, through: :categorizings
end
vendor.rb
class Vendor < ActiveRecord::Base
has_many :categorizings, :as => :categorizable, inverse_of: :vendor, dependent: :destroy
has_many :categories, through: :categorizings
end
cars_controller.rb
class CarsController < ApplicationController
def new
@car = Car.new
end
def create
@car = current_user.cars.build(car_params)
if @car.save
redirect_to @car
else
render 'new'
end
end
private
def car_params
params.require(:car).permit(:name, :details, :type, :category_ids => [] )
end
end
schema.rb
create_table "categories", force: :cascade do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "categorizings", force: :cascade do |t|
t.integer "category_id"
t.integer "categorizable_id"
t.string "categorizable_type"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "categorizings", ["categorizable_type", "categorizable_id"], name: "index_categorizings_on_categorizable_type_and_categorizable_id", using: :btree
这就是我在表单中的内容
<%= f.collection_select :category_ids, Category.all, :id, :name %>
我收到此错误: 不允许的参数:category_ids
我现在很困惑,迷失在模型中。不知道这是不是最好的方法。如果有人能告诉我哪里出错了,我会很高兴。
【问题讨论】:
-
能否也包括db/schema.rb的内容?
-
添加了 schema.rb @pdoherty926
标签: ruby-on-rails ruby-on-rails-4 has-many-through polymorphic-associations form-for