0

我们有一个 Rails 应用程序,它不断产生我们无法追踪的错误。问题似乎出在路由上,但我们不确定发生了什么。输出路由文件如下所示:

  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 中有一组管理控制器。我们一直看到的问题是,当用户访问诸如 admin/bookings、admin/bookings/step_1 或 admin/events 之类的 URL 时。有时 url 工作得很好,但有时我可以从日志文件中看到类似以下的事情发生:

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]

从最后一个示例中,您可以看到请求的 urladmin/events应该已经点击了#indexin ,Admin::EventsController但它呈现了in 中的#index操作EventsController

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

4

1 回答 1

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]在正确的位置为所有控制器添加路由。

于 2009-02-01T19:40:41.320 回答