我正在關注使用複選框使用複選框創建關聯記錄的HABTM Railscast#17的。使用has_many複選框創建的重複連接表記錄無法取消選擇/銷燬它們
我終於得到了我的記錄保存到連接表....但我無法通過檢查或取消選中一個框來控制它們....意味着取消選中該記錄不會執行任何操作並將選中的記錄保留爲另一個記錄。
我有3張桌子。
:project has_many :restrictions, :through => :project_restrictions
我想什麼,能夠做的是,我可以檢查創建複選框的數組:project_restrictions。 ..或,如果我取消選中它們,則刪除:project_restriction記錄。
現在它只是保存多個記錄,不刪除它們,如果我取消選中它們。
我已經把我所有的邏輯在ProjectsController和正在運行的方法添加:通過自定義PATCH方法project_restrictions稱爲「add_restrictions」。這應該是一個POST嗎?我無法通過添加關聯記錄或僅發佈關聯記錄來確定我是否在PATCHING Project中。
我的連接表有一個:ID沒有:時間戳 ....我不知道這是否事項....很明顯,我是新的。
我使用的鐵軌4
我的模型
class Project < ActiveRecord::Base
has_many :project_restrictions, dependent: :destroy
has_many :restrictions, :through => :project_restrictions
accepts_nested_attributes_for :project_restrictions, allow_destroy: true, :reject_if => :all_blank
class Restriction < ActiveRecord::Base
has_many :projects, :through => :project_restrictions
has_many :project_restrictions, dependent: :destroy
class ProjectRestriction< ActiveRecord::Base
belongs_to :restriction
belongs_to :project
end
**我PATCH控制器的方法創建相關的記錄**
def add_restrictions
@diet = Restriction.k1.order(:name)
@project = Project.find(params[:p])
params[:project][:restriction_ids].reject!(&:empty?).each do |restriction|
@proj_rule = ProjectRestriction.create!(:project_id => @project.id, :restriction_id => restriction)
@proj_rule.save
end
respond_to do |format|
format.html { redirect_to t1s3_path(:p => @project.id) ,
notice: 'Success! You added restrictions!' }
format.json {}
end
end
我的形式
<%= form_for @project, url: add_restrictions_path(:p => @project.id) do |f| %>
<div class=" fields ">
<%= hidden_field_tag "project[restriction_ids][]", nil %>
<% @diet.each do |restriction| %>
<%= check_box_tag "project[restriction_ids][]", restriction.id,
@project.restriction_ids.include?(restriction.id), id: dom_id(restriction)%>
<%= label_tag dom_id(restriction), restriction.name %><br>
<% end %>
</div>
<%= f.submit %>
UPDATE:
我已經更新我的模型有「uniq的和inverse_of ....但是我仍然創造重複的記錄,無法通過取消選中該複選框
class Project < ActiveRecord::Base
has_many :project_restrictions -> { uniq }, dependent: :destroy, inverse_of: :project
has_many :restrictions, -> { uniq }, through: :project_restrictions
accepts_nested_attributes_for :project_restrictions, allow_destroy: true, :reject_if => :all_blank
class Restriction < ActiveRecord::Base
has_many :projects, -> { uniq }, through: :project_restrictions
has_many :project_restrictions, -> { uniq }, dependent: :destroy, inverse_of: :restriction
class ProjectRestriction< ActiveRecord::Base
belongs_to :restriction, -> { uniq }, inverse_of: :project_restrictions
belongs_to :project, -> { uniq }, inverse_of: :project_restrictions
end
謝謝你......但我只是得到這個「使用{:locale => [:en],:formats => [:html],:variants => [],:處理程序=> [:erb,:builder,:]缺少模板projects/add_restrictions,application/add_restrictions:原始,:紅寶石,:jbuilder,:咖啡]}。搜索:*「/ app/views」*「/Library/Ruby/Gems/2.0.0/gems/devise-3.2.4/app/views」 – NothingToSeeHere 2014-10-30 05:28:46
@NothingToSeeHere在剛剛添加之前,您是否收到同樣的錯誤這取代了這一點,並保持相同的方法,我會更新我的答案,請檢查它 – anusha 2014-10-30 05:33:54
該代碼是缺少的東西...它不斷地調用另一個'keyword_end'......當我添加另一個'end'時,它說這個「在這個動作中渲染和/或重定向被多次調用。請注意,你可能只能調用渲染或重定向,並且至多每個動作一次,還要注意,重定向和渲染都不會終止動作的執行,所以如果你想在重定向後退出動作,你需要做一些類似「redirect_to(...)並返回」。 – NothingToSeeHere 2014-10-30 14:00:38