0

我有一个用户模型,它可以通过丰富的连接表有很多假期。我在视图上的销毁语句是从假期表中删除,而不是应该从 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 

有任何想法吗?谢谢!!!!

4

2 回答 2

1

你应该使用@user.user_holidays而不是@user.holidays

@user_holiday = @user.user_holidays.where(holiday_id: params[:id])
于 2012-05-23T05:19:59.620 回答
0

(我的错误没有正确阅读你的问题)

 @user = User.find(params[:user_id]) 
 @user.holidays.delete(@user.holidays.find(params[:id]))
于 2012-05-23T04:08:00.677 回答