Rails路由错误与命名空间项目

时间:2009-01-21 14:06:06

标签: ruby-on-rails routes rails-routing ruby-on-rails-2

我们有一个rails应用程序,不断产生我们无法追踪的错误。问题似乎在于路由,但我们不确定发生了什么。 Out routes文件看起来像:

  ActionController::Routing::Routes.draw do |map|
    # Default
    map.home '', :controller => "home"

    # Admin

    map.admin_home 'admin', :controller => 'admin/admin', :action => 'index'
    map.admin_login 'admin/login', :controller => 'admin/admin', :action => 'login'
    map.admin_reminder 'admin/forgot', :controller => 'admin/admin', :action => 'reminder'
    map.namespace :admin do |admin|
      admin.resources :bookings,
        :collection => {
          :archive => :get, :reports => :get, :calendar => :get,
          :step_1 => :any, :step_1a => :any, :step_2 => :any, :step_3 => :any, :confirmation => :any }
      admin.resources :events,
        :member => { :status => :get }
      admin.resources :blogs,
        :collection => { :archive => :get }
      admin.resources :blog_replies,
        :member => { :publish => :get }
      admin.resources :minutes,
        :collection => { :archive => :get }
      admin.resources :businesses
      admin.resources :business_categories
      admin.resources :users
      admin.resources :pricings
      admin.backups 'backups', :controller => 'admin/backups'
      admin.download_backup 'backups/download', :controller => 'admin/backups', :action => 'download'
    end
    map.admin 'admin/:action', :controller => 'admin/admin'

    map.connect 'members', :controller => 'admin/admin', :action => 'redirect_to_index'
    map.connect 'members/login', :controller => 'admin/admin', :action => 'redirect_to_index'
    map.connect 'account', :controller => 'admin/admin', :action => 'redirect_to_index'
    map.connect 'account/login', :controller => 'admin/admin', :action => 'redirect_to_index'
    map.connect 'home', :controller => 'admin/admin', :action => 'redirect_to_index'
    map.connect 'home/login', :controller => 'admin/admin', :action => 'redirect_to_index'

    map.blog 'blog/:permalink', :controller => 'blogs', :action => 'show'
    map.connect 'blog/:id', :controller => 'blogs', :action => 'show'
    map.connect 'book-online', :controller => 'bookings', :action => 'step_1'
    map.connect 'book-online/:action', :controller => 'bookings'
    # Defaults
    map.connect ':controller/:action/:id'
    map.connect "*anything", :controller => "public", :action => "unknown_request"
  end

我们在app / controllers中有一组公共控制器,在app / controllers / admin中有一组管理员控制器。我们一直看到的问题是当用户访问管理员/预订,管理员/预订/步骤1或管理员/事件等网址时。有时网址工作得很好,但有时我可以从日志文件中看到类似以下内容的情况:

ActionController::UnknownAction (No action responded to index):

其他时候我们会得到类似的东西:

Processing EventsController#index (for [filtered] at 2009-01-21 10:54:38) [GET]
Session ID: [filtered]
Parameters: {"action"=>"index", "controller"=>"admin/events"}
Rendering template within layouts/public
Rendering events/index
Completed in 0.00863 (115 reqs/sec) | Rendering: 0.00338 (39%) | DB: 0.00000 (0%) | 200 OK [http://www.gresfordtrust.org/admin/events]

从上一个示例中,您可以看到请求的网址为admin/events,该网址应该点击#index中的Admin::EventsController,而是在#index中呈现EventsController操作1}}。

我们正在使用Rails 2.0.2运行应用程序。

1 个答案:

答案 0 :(得分:2)

您没有为EventsController配置路由,因此您的错误正在发生,因为某些请求正在降至默认路由map.connect ':controller/:action/:id'

发生这种情况是因为某人/某事正在发送未为您的AdminEventsController配置的HTTP方法的请求。您的admin.resources :events, :member => { :status => :get }将符合以下要求:

GET /admin/events
GET /admin/events/<id>
GET /admin/events/<id>/status
POST /admin/events
PUT /admin/events/<id>
DELETE /admin/events/<id>

其他任何东西都会落入默认路线。因此,如果您在此控制器上看到ActionController::UnknownAction,请查找使用错误HTTP方法的请求。

您奇怪的日志消息的来源几乎肯定是一个类似这样的请求:

GET /admin/events/index

解决方案是完全摆脱该默认路由,并确保为正确位置的所有控制器添加resource[s]路由。