2011-04-05 71 views
2

我有以下的jQuery函數,當有人點擊了end_meeting按鈕發送POST請求到服務器,以通知我:阿賈克斯POST在軌自定義操作

$("input#end_meeting").bind('click', function(e) { 
    var user_id = 500; 
    var status = 0; 

    $.ajax({ 
      type: 'POST', 
      url: '/meetings/user_left', 
      data: { user_id : user_id, user_status : status }, 
      dataType: 'html', 
      success: function(xhr, textStatus) { 
       alert('awesome!!!'); 
      }, 
      error: function(xhr, textStatus, errorThrown) { 
          alert('something went wrong'); 
      } 

     }); 
}); 

,在我的routes.rb我有此:

match 'meetings/user_left' => 'meetings#user_left' 
resources :meetings 

對應於該控制器的方法在MeetingsController:

def user_left 
    #do stuff 
end 

然而,當我執行AJAX POST方法我收到以下錯誤:

AbstractController::ActionNotFound (The action 'user_left' could not be found for MeetingsController): 

我重新格式化與

post 'meetings/user_left' => 'meetings#user_left' 

但仍沒有運氣的路線聲明。有誰知道爲什麼我無法創建自定義類方法的路線?

編輯1

這裏的服務器日誌完全轉儲。對不起有點混亂。

Started POST "/meetings/user_left" for 127.0.0.1 at 2011-04-05 14:06:00 -0700 

AbstractController::ActionNotFound (The action 'user_left' could not be found for MeetingsController): 
    actionpack (3.0.3) lib/abstract_controller/base.rb:115:in `process' 
    actionpack (3.0.3) lib/abstract_controller/rendering.rb:40:in `process' 
    actionpack (3.0.3) lib/action_controller/metal.rb:138:in `dispatch' 
    actionpack (3.0.3) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch' 
    actionpack (3.0.3) lib/action_controller/metal.rb:178:in `block in action' 
    actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:62:in `call' 
    actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:62:in `dispatch' 
    actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:27:in `call' 
    rack-mount (0.6.13) lib/rack/mount/route_set.rb:148:in `block in call' 
    rack-mount (0.6.13) lib/rack/mount/code_generation.rb:93:in `block in recognize' 
    rack-mount (0.6.13) lib/rack/mount/code_generation.rb:68:in `optimized_each' 
    rack-mount (0.6.13) lib/rack/mount/code_generation.rb:92:in `recognize' 
    rack-mount (0.6.13) lib/rack/mount/route_set.rb:139:in `call' 
    actionpack (3.0.3) lib/action_dispatch/routing/route_set.rb:492:in `call' 
    warden (0.10.7) lib/warden/manager.rb:35:in `block in call' 
    warden (0.10.7) lib/warden/manager.rb:34:in `catch' 
    warden (0.10.7) lib/warden/manager.rb:34:in `call' 
    haml (3.0.25) lib/sass/plugin/rack.rb:41:in `call' 
    actionpack (3.0.3) lib/action_dispatch/middleware/best_standards_support.rb:17:in `call' 
    actionpack (3.0.3) lib/action_dispatch/middleware/head.rb:14:in `call' 
    rack (1.2.1) lib/rack/methodoverride.rb:24:in `call' 
    actionpack (3.0.3) lib/action_dispatch/middleware/params_parser.rb:21:in `call' 
    actionpack (3.0.3) lib/action_dispatch/middleware/flash.rb:182:in `call' 
    actionpack (3.0.3) lib/action_dispatch/middleware/session/abstract_store.rb:149:in `call' 
    actionpack (3.0.3) lib/action_dispatch/middleware/cookies.rb:295:in `call' 
    activerecord (3.0.3) lib/active_record/query_cache.rb:32:in `block in call' 
    activerecord (3.0.3) lib/active_record/connection_adapters/abstract/query_cache.rb:28:in `cache' 
    activerecord (3.0.3) lib/active_record/query_cache.rb:12:in `cache' 
    activerecord (3.0.3) lib/active_record/query_cache.rb:31:in `call' 
    activerecord (3.0.3) lib/active_record/connection_adapters/abstract/connection_pool.rb:353:in `call' 
    actionpack (3.0.3) lib/action_dispatch/middleware/callbacks.rb:46:in `block in call' 
    activesupport (3.0.3) lib/active_support/callbacks.rb:415:in `_run_call_callbacks' 
    actionpack (3.0.3) lib/action_dispatch/middleware/callbacks.rb:44:in `call' 
    rack (1.2.1) lib/rack/sendfile.rb:107:in `call' 
    actionpack (3.0.3) lib/action_dispatch/middleware/remote_ip.rb:48:in `call' 
    actionpack (3.0.3) lib/action_dispatch/middleware/show_exceptions.rb:46:in `call' 
    railties (3.0.3) lib/rails/rack/logger.rb:13:in `call' 
    rack (1.2.1) lib/rack/runtime.rb:17:in `call' 
    activesupport (3.0.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call' 
    rack (1.2.1) lib/rack/lock.rb:11:in `block in call' 
    <internal:prelude>:10:in `synchronize' 
    rack (1.2.1) lib/rack/lock.rb:11:in `call' 
    actionpack (3.0.3) lib/action_dispatch/middleware/static.rb:30:in `call' 
    railties (3.0.3) lib/rails/application.rb:168:in `call' 
    railties (3.0.3) lib/rails/application.rb:77:in `method_missing' 
    railties (3.0.3) lib/rails/rack/log_tailer.rb:14:in `call' 
    rack (1.2.1) lib/rack/content_length.rb:13:in `call' 
    rack (1.2.1) lib/rack/handler/webrick.rb:52:in `service' 
    /Users/chris/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/httpserver.rb:111:in `service' 
    /Users/chris/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/httpserver.rb:70:in `run' 
    /Users/chris/.rvm/rubies/ruby-1.9.2-p136/lib/ruby/1.9.1/webrick/server.rb:183:in `block in start_thread' 

EDIT 2

我結束了黑客的修復。鐵軌似乎能夠路由以下

match 'meetings' => 'meetings#index', :via => :post 

我把一個特殊的標誌在PARAMS從Ajax調用識別的職位。仍然是沒有意義的我,爲什麼軌只想路由到REST風格的路線....

+1

凡'tutor_left'定義? – McStretch 2011-04-05 21:13:47

+0

糟糕。我有兩個我合併的代碼版本。再次,更多的錯別字..我現在編輯它是一致的。 – kikster 2011-04-06 07:47:02

回答

1

我想嘗試改變,以這樣的路線:

resources :meetings do 
    post 'user_left', :on => :collection 
end 
+0

嗯...試過了,沒有運氣。它仍然在說'user_left'找不到。有沒有理由爲什麼軌道不會路由到自定義操作? – kikster 2011-04-06 09:07:57

+0

你確定「會議」使用正確的控制器?您似乎引用了2個不同的控制器 - OnlineMeetingsController和MeetingsController。 – 2011-04-06 13:19:07

+0

是的。這都是MeetingsController。我花了數小時試圖弄清楚這一點......我已經看到了很多關於如何路由到自定義路由的路由示例,但這裏沒有運氣。 – kikster 2011-04-07 05:23:54