Ruby on rails 将属性添加到多个Rails列表4
我想创建属性。然后将其添加到许多列表中。一个属性可以位于多个列表中 这是我的模型:Ruby on rails 将属性添加到多个Rails列表4,ruby-on-rails,Ruby On Rails,我想创建属性。然后将其添加到许多列表中。一个属性可以位于多个列表中 这是我的模型: class List < ActiveRecord::Base has_many :propertyships has_many :properties, :through => :propertyships end class Propertyship < ActiveRecord::Base belongs_to :list belongs_to :property end
class List < ActiveRecord::Base
has_many :propertyships
has_many :properties, :through => :propertyships
end
class Propertyship < ActiveRecord::Base
belongs_to :list
belongs_to :property
end
class Property < ActiveRecord::Base
has_many :propertyships
has_many :lists, :through => :propertyships
end
类列表:propertyships
结束
类Propertyship:propertyships
结束
属性/show.html.erb
<%= form_for @property do |f| %>
<% List.all.each do |list| %>
<%= check_box_tag "property[list_ids][]", list.id,@property.list_ids.include?(list.id) %>
<%= list.name %>
<% end %>
<%= f.submit %>
<% end %>
属性未添加到列表中。
我做错了什么???使用而不是手动创建输入:
<%= form_for @property do |f| %>
<%= f.collection_check_boxes(:list_ids, List.all, :id, :name) %>
<%= f.submit %>
<% end %>
class PropertiesController < ApplicationController
# ...
def create
@property = Property.new(property_params)
# ...
end
# ...
private
def property_params
params.require(:property)
.permit(:foo, :bar, list_ids: [])
end
end