Ruby on rails 4 正在创建的重复联接表记录具有多个复选框,可以';不要取消选中/销毁它们
我正在遵循HABTM Railscast 17关于使用复选框使用联接表创建关联记录的 我终于得到了要保存到联接表的记录……但我无法通过选中或取消选中一个框来控制它们……这意味着取消选中该记录不会产生任何效果,而保持该记录处于选中状态会生成另一个重复记录 我有三张桌子Ruby on rails 4 正在创建的重复联接表记录具有多个复选框,可以';不要取消选中/销毁它们,ruby-on-rails-4,checkbox,associations,has-many-through,jointable,Ruby On Rails 4,Checkbox,Associations,Has Many Through,Jointable,我正在遵循HABTM Railscast 17关于使用复选框使用联接表创建关联记录的 我终于得到了要保存到联接表的记录……但我无法通过选中或取消选中一个框来控制它们……这意味着取消选中该记录不会产生任何效果,而保持该记录处于选中状态会生成另一个重复记录 我有三张桌子 :project has_many :restrictions, :through => :project_restrictions 我希望能够做的是有一系列复选框,我可以选中这些复选框来创建:project\u限制。。或
:project has_many :restrictions, :through => :project_restrictions
我希望能够做的是有一系列复选框,我可以选中这些复选框来创建:project\u限制。。或者,如果我取消选中它们,则删除:project\u限制记录
现在它只保存多条记录,如果我取消选中一条,它不会删除它们
我已将所有逻辑放在ProjectsController中,并正在通过名为“add_restrictions”的自定义修补程序方法运行添加:project_restrictions的方法。这应该是一个帖子吗?我不知道我是通过添加相关记录还是仅仅发布相关记录来修补项目
我的联接表有一个:id,没有:时间戳。。。。我不知道这是否重要……显然,我是新来的
我使用的是rails 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
和我的表格
<%= 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 %>
@project.id)do | f |%>
更新:
我已经将我的模型更新为“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
class项目{uniq},dependent::destroy,inverse\u of::project
有很多:限制,->{uniq},通过::project\u限制
接受\u嵌套的\u属性\u用于:项目\u限制,允许\u销毁:true,:拒绝\u如果=>:全部\u为空
类限制{uniq},通过::project\u限制
有很多:project\u限制、>{uniq}、dependent::destroy、reverse\u of::restriction
类ProjectRestriction{uniq},逆的::项目限制
属于:项目,->{uniq},与::项目限制相反
结束
在您的添加限制方法中更改此选项,然后重试
def add_restrictions
@diet = Restriction.k1.order(:name)
@project = Project.find(params[:p])
params[:project][:restriction_ids].reject!(&:empty?).each do |restriction|
@projectrestrictions = ProjectRestriction.all
@projectrestrictions.each do |prj|
if prj.restriction_id == restriction
ProjectRestriction.destroy_all(prj.id)
else
@proj_rule = ProjectRestriction.create!(:project_id => @project.id, :restriction_id => restriction)
@proj_rule.save
end
end
respond_to do |format|
format.html { redirect_to t1s3_path(:p => @project.id ) ,
notice: 'Success! You added restrictions!' }
format.json {}
end
end
谢谢你…但我刚刚得到了这个“缺少模板项目/添加限制,应用程序/添加限制,{:locale=>[:en],:formats=>[:html],:variants=>[],:handlers=>[:erb,:builder,:raw,:ruby,:jbuilder,:coffee]}。搜索:“/app/views”*“/Library/Ruby/Gems/2.0.0/Gems/devise-3.2.4/app/views”@nothingtosehere在您只需添加此项而不添加此项并在该方法中保持不变之前,您是否遇到了相同的错误?我将更新我的答案。请检查代码是否缺少某些内容…它一直在调用另一个“关键字”\u end”。。。当我添加另一个
end
时,它会这样说“在此操作中多次调用渲染和/或重定向。请注意,您只能调用render或redirect,每个操作最多只能调用一次。另外请注意,重定向和渲染都不会终止操作的执行,因此如果您想在重定向后退出操作,则需要执行类似“重定向到(…)并返回”的操作。“因此,我尝试管理重定向并尝试了以下操作:`respond_to do | format | return redirect_to t1s3_path(:p=>@project.id)flash[:notice]=”更新事件!!!!“format.json{}end`它保存了,但是它给了每个记录相同的限制\u id…所以它实际上不起作用。
def add_restrictions
@diet = Restriction.k1.order(:name)
@project = Project.find(params[:p])
params[:project][:restriction_ids].reject!(&:empty?).each do |restriction|
@projectrestrictions = ProjectRestriction.all
@projectrestrictions.each do |prj|
if prj.restriction_id == restriction
ProjectRestriction.destroy_all(prj.id)
else
@proj_rule = ProjectRestriction.create!(:project_id => @project.id, :restriction_id => restriction)
@proj_rule.save
end
end
respond_to do |format|
format.html { redirect_to t1s3_path(:p => @project.id ) ,
notice: 'Success! You added restrictions!' }
format.json {}
end
end