Rails 莫名的 routes 问题

ruohanc · 2012年04月29日 · 最后由 ruohanc 回复于 2012年04月29日 · 2771 次阅读

只贴相关的

# routes.rb
 namespace :admin do
    resources :users
    resources :projects
  end

  resources :projects do
    resources :teams
  end

rake routes 太长了,完整的去 pastebin 看,这里只写两条我认为相关的

# rake routes
admin_projects GET  /admin/projects(.:format)   admin/projects#index
projects    GET    /projects(.:format)          projects#index

最后是 log

Started GET "/admin/projects" for 172.16.164.1 at 2012-04-28 14:32:22 +0800
Processing by ProjectsController#index as HTML
  User Load (0.3ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
  Rendered layouts/_subnav.html.haml (4.8ms)
  Project Load (0.3ms)  SELECT "projects".* FROM "projects" ORDER BY updated_at DESC
  CACHE (0.0ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
  Rendered projects/_index.html.haml (120.0ms)
  Rendered projects/index.html.haml within layouts/application (133.6ms)
  Rendered layouts/_nav.html.haml (6.6ms)
  Rendered layouts/_flash.html.haml (3.8ms)
Completed 200 OK in 353ms (Views: 256.2ms | ActiveRecord: 2.5ms)[2012-04-28 14:32:22] WARN  Could not determine content-length of response body. Set content-length of the response or set Response#chunked = true

问题是为什么我访问了 /admin/projects 却用了 projects#index, 而不是 admin/projects#index

controller 里面应该是 Admin::PojectsController。

namespace :admin do
  resources :posts, :comments
end

This will create a number of routes for each of the posts and comments controller. For Admin::PostsController, Rails will create:

HTTP Verb Path action named helper GET /admin/posts index admin_posts_path

#1 楼 @cantin = =~ 最后发现是命名问题...我的 Project 少了一个 S

需要 登录 后方可回复, 如果你还没有账号请 注册新账号