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 %>
不行的,同样的错误 –
的输出是什么? – knotito
Sry为已故的答案@knotito!我得到这个输出 'ActionController :: RoutingError(没有路由匹配[GET]“/ collections/89”)' –