在Rails 4中未检测到嵌套资源的路由

时间:2014-01-10 14:16:26

标签: ruby-on-rails ruby-on-rails-4 nested-resources

# routes.rb
resources :assets, only: [:new, :create, :delete]

# asset.rb
class Asset < ActiveRecord::Base
  belongs_to :post
end

# rake routes
         Prefix Verb   URI Pattern                            Controller#Action
post_comments POST   /posts/:post_id/comments(.:format)     comments#create
new_post_comment GET    /posts/:post_id/comments/new(.:format) comments#new
       posts GET    /posts(.:format)                       posts#index
             POST   /posts(.:format)                       posts#create
    new_post GET    /posts/new(.:format)                   posts#new
   edit_post GET    /posts/:id/edit(.:format)              posts#edit
        post GET    /posts/:id(.:format)                   posts#show
             PATCH  /posts/:id(.:format)                   posts#update
             PUT    /posts/:id(.:format)                   posts#update
             DELETE /posts/:id(.:format)                   posts#destroy
   post_form POST   /post_form(.:format)                   posts#form
        root GET    /                                      posts#index

assets没有显示路由,但我需要能够删除它们而不指定post_id,因为它们可以在没有帖子的情况下存在(为了能够上传文件和访问而必需)这些文件在新帖子中。)

路由assets#newassets#create工作得很好,但不是assets#destroy(我收到错误提示DELETE /assets/<id>未定义。

请帮忙! : - )

1 个答案:

答案 0 :(得分:6)

为了破坏工作,

更改route.rb中的行(请注意:destroy而不是:delete

resources :assets, only: [:new, :create, :destroy]