属性有很多列表。列表有很多属性。用户可以向列表添加属性,此处用户必须是要添加的属性的所有者。
这是我的代码:
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
在视图中:
<%= simple_form_for(@list, url: add_property_list_path, :method => :put) do |f| %>
<%= f.collection_check_boxes(:property_ids, @user_all_properties, :id, :street_address) %>
<%= f.submit "Add Property" %>
<% end %>
路线中的:
resources :lists do
member do
put 'add_property'
end
end
控制器中的:
def add_property
@list = List.find(params[:id])
@list.update_attributes(list_params)
end
private
def list_params
params.require(:list).permit(:name, :user_id, property_ids: [])
end
当我检查属性并单击“添加属性”按钮作为其他用户时,它添加了已检查的属性,但删除了我在该特定列表中的旧属性。我做错了什么?