我有个大问题!
我有三个模特,学校,秘书和Schools_Secretary。School_secretary建立了一个连接模型,并运行良好。但是在添加了一个记录之后,我不能删除同样的内容。(有人能帮我吗?
我不想删除依赖销毁,因为学校和秘书从未删除过,我只想删除表schools_secretaries上的联接模型记录
Secretary.rb
class Secretary < ActiveRecord::Base
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :schools_secretaries
has_many :schools, :through => :schools_secretaries,:uniq => true
endSchool.rb
class School < ActiveRecord::Base
has_many :schools_secretaries
has_many :secretaries, :through => :schools_secretaries,:uniq => true
endschools_secretary.rb
class SchoolsSecretary < ActiveRecord::Base
belongs_to :secretary
belongs_to :school
endroutes.rb访问本地主机/学校/1/秘书/1
resources :schools do
resources :schools_secretaries
resources :secretaries
devise_for :secretaries, :controllers => {:registrations => "registrations"}结束
show.html.erb .操作新工作很好,我可以在表中插入寄存器
<%= form_for [@school,SchoolsSecretary.new], :method => :post,:remote => true do %>
<%= hidden_field_tag :school_id, @school.id %>
<%= hidden_field_tag :secretary_id, @secretary.id %>
<%= submit_tag 'Adicionar' %>
<% end %>schools_secretaries_controller.rb
def create
@school = School.find(params[:school_id])
@secretary = Secretary.find(params[:secretary_id])
schools_secretary_params = { :school_id => @school.id, :secretary_id => @secretary.id }
@schools_secretary = SchoolsSecretary.new(schools_secretary_params)
respond_to do |format|
if @schools_secretary.save
format.html { redirect_to @schools_secretary, notice: 'Schools secretary was successfully created.' }
#format.json { render action: 'show', status: :created, location: @schools_secretary }
else
format.html { render action: 'new' }
format.json { render json: @schools_secretary.errors, status: :unprocessable_entity }
end
end
end发布于 2014-03-05 06:30:58
要删除联接表记录,让我们假设您有“学校”和“秘书”。现在试试下面的代码。
@school.secretaries.delete(@secretary)https://stackoverflow.com/questions/22190260
复制相似问题