1

ネストされたリソースに破棄ボタンを追加しようとすると、次のエラーが発生します。No route matches [DELETE] "/users/1/2/4/5/holidays/7"

ビュー、ルート、モデル、コントローラーの関連部分は次のとおりです。

<% @user.holidays.each do |h| %>
  <td><%= h.name %></td>
  <td><%= h.date %></td>
  <td>
    <%= button_to('Destroy', user_holiday_path(@user.holidays), :method => 'delete', :class => 'btn btn-large btn-primary')  %>
  </td>
<% end %>

ルート

resources :users do
  resources :interests
  resources :holidays
end

モデル

class User < ActiveRecord::Base
  has_many :holidays, :through => :user_holidays
end

class UserHoliday < ActiveRecord::Base
  attr_accessible :holiday_id, :user_id
  belongs_to :user
  belongs_to :holiday
end

class Holiday < ActiveRecord::Base
  attr_accessible :name, :date
  has_many :user_holidays
  has_many :users, :through => :user_holidays

end

コントローラ

class HolidaysController < ApplicationController
  def index
    @user_holidays = Holiday.find(params[:user_id])
    @holidays = @user_holidays.holidays
  end

  def new
  end

  def show
    @holiday = Holiday.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @holiday }
    end
  end

  def destroy
    @holiday = Holiday.find(params[:id])
    @holiday.destroy
  end
end

ありがとう!!!

4

1 に答える 1

2

これを変更します:
<%= button_to('Destroy', user_holiday_path(@user.holidays), :method => 'delete', :class => 'btn btn-large btn-primary') %>
これに:
<%= button_to('Destroy', user_holiday_path(h), :method => 'delete', :class => 'btn btn-large btn-primary') %>

アップデート

破棄アクションを:
@holiday = Holiday.find(params[:id])からに変更します
@user_holiday = UserHoliday.find(params[:id])

そしてあなたの見解では:に
変更
<% @user.holidays.each do |h| %> します

<% @user.user_holidays.each do |h| %>

関連付けには修正が必要であり、次のようにする必要があります。
user has_many user_holidays
user_holiday has_one holiday
user_holidays belongs_to user

hオブジェクトを介して名前と休日にアクセスできます。
h.holiday.name
h.holiday.date

于 2012-05-22T23:20:36.773 に答える