2015-10-15 141 views
0

我想刪除項目頁面上的「集合」。當我點擊刪除鏈接時,出現此錯誤:Rails 4 Ajax:刪除嵌套對象

No route matches [GET] "/collections/87"

我的錯誤在哪裏?


我的文件:

的routes.rb

resources :projects do 
    resources :collections, :except => [:update, :destroy] 
end 

resources :collections, :only => [:update, :destroy] 

collection.rb

class Collection < ActiveRecord::Base 
    belongs_to :project 

    ... 
    validates :project, presence: true 
end 

project.rb

class Project < ActiveRecord::Base 

    has_many :collections, dependent: :destroy 

    accepts_nested_attributes_for :collections, reject_if: proc() { | attrs | attrs[ 'title' ] .blank? }, allow_destroy: true 

end 

collections_controller.rb

class CollectionsController < ApplicationController 
    ... 

    respond_to :html, :js 

    def destroy 
    @project = @collection.project_id 
    @collection.destroy 

    respond_to do |format| 
     format.html { redirect_to @project } 
     format.js # render collections/destroy.js.erb 
    end 

    end 

projects_controller.rb

class ProjectsController < ApplicationController 

    ... 

    respond_to :html, :js 

    def index 
    @projects = Project.all 
    respond_with(@projects) 
    end 

    def show 
    @collection = Collection.new 
    @collections = @project.collections.order("title ASC") 
    end 

    def destroy 
    @project.destroy 
    respond_with(@project) 
    end 

... 

/projects/show.html.erb

<% @collections.each do |collection| %> 
    <%= link_to 'Destroy Collection', collection, method: :delete, remote: true %> 
<% end %> 

回答

0

的問題是您的收藏控制器,它不上獲取有問題的集合。

的變化可能是:

class CollectionsController < ApplicationController 
    ... 

    respond_to :html, :js 

    def destroy 
    @project = Project.find(params[:project_id]) 

    @project.collections.find(params[:id]).delete 

    respond_to do |format| 
     format.html { redirect_to @project } 
     format.js # render collections/destroy.js.erb 
    end 

    end 
+0

不行的,同樣的錯誤 –

+0

的輸出是什麼? – knotito

+0

Sry爲已故的答案@knotito!我得到這個輸出 'ActionController :: RoutingError(沒有路由匹配[GET]「/ collections/89」)' –