我有一个大问题!
我有三个模特,学校,秘书和学校_秘书。 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
end
School.rb
class School < ActiveRecord::Base
has_many :schools_secretaries
has_many :secretaries, :through => :schools_secretaries,:uniq => true
end
schools_secretary.rb
class SchoolsSecretary < ActiveRecord::Base
belongs_to :secretary
belongs_to :school
end
routes.rb to acess localhost / schools / 1 / secretaryaries / 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
答案 0 :(得分:1)
要删除联接表记录,我们假设您拥有@school和@secretary。现在尝试以下代码。
@school.secretaries.delete(@secretary)