我是 Ruby on Rails 的初学者,我遇到了一个问题。我正在尝试向我的应用添加评论。一切正常,但是当我将此代码添加到 routes.rb
resources :galleries do
resources :comments, module: :galleries
end
resources :articles do
resources :comments, module: :articles
end
我无法更新任何图库或文章。我的整个路线.rb:
Rails.application.routes.draw do
devise_for :users
resources :galleries do
resources :comments, module: :galleries
end
resources :articles do
resources :comments, module: :articles
end
match ':controller(/:action(/:id))(.:format)', via: [:post, :get]
root 'public#index'
end
看起来请求 (articles/12?page_id=4) 正在落入匹配语句并且没有被您的资源接收。你的比赛声明:
match ':controller(/:action(/:id))(.:format)', via: [:post, :get]
正在匹配“文章”作为 Controller ,并寻找“12”作为 Action ,但显然找不到。
我会废弃 match 语句并永久地严格使用命名路由或足智多谋的路由,或者至少在调试时。接下来,运行
rake routes
然后检查结果。从你的 routes.rb 我猜你会有这样一行:
article GET /articles/:id(.:format) articles#show
如果确实如此,请确保您在文章 Controller 中定义了 show() 方法,并验证日志是否显示了针对它的 GET 请求。
我是一名优秀的程序员,十分优秀!