我有一个用户模型,它可以通过丰富的连接表有很多假期。我在视图上的销毁语句是从假期表中删除,而不是应该从 user_holidays 表中删除。见下文:
class HolidaysController < ApplicationController
def destroy
@user = User.find(params[:user_id])
@user_holiday = @user.holidays.find(params[:id])
@user_holiday.destroy
redirect_to @user
end
end
这是查看按钮:
<% @user.holidays.each do |hld| %>
<td><%= hld.name %></td>
<td><%= hld.date %></td>
<td>
<%= button_to('Destroy', user_holiday_path(@user, hld), :method => 'delete', :class => 'btn btn-large btn-primary') %>
楷模:
class User < ActiveRecord::Base
has_many :user_holidays
has_many :holidays, :through => :user_holidays
class UserHoliday < ActiveRecord::Base
attr_accessible :holiday_id, :user_id
belongs_to :user
belongs_to :holiday
class Holiday < ActiveRecord::Base
attr_accessible :name, :date
has_many :user_holidays
has_many :users, :through => :user_holidays
有任何想法吗?谢谢!!!!