简体   繁体   English

Ajax POST到Rails中的自定义操作

[英]Ajax POST to custom action in rails

I have the following JQuery function that sends a post request to the server to notify me when someone has clicked an end_meeting button: 我有以下JQuery函数,当有人单击end_meeting按钮时,它将向服务器发送发布请求以通知我:

$("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');
            }

        });
});

and in my routes.rb I have this: 在我的routes.rb中,我有这个:

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

which corresponds to this controller method in MeetingsController: 对应于MeetingsController中的该控制器方法:

def user_left
  #do stuff
end

However, when I execute the AJAX post method I get the following error: 但是,当我执行AJAX post方法时,出现以下错误:

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

I've reformatted the routes declaration with 我用以下命令重新格式化了路线声明

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

but still no luck. 但仍然没有运气。 Does anyone know why I'm not able to create a route to a custom class method? 有谁知道为什么我无法创建到自定义类方法的路由?

EDIT 1 编辑1

Here's a full dump of the server logs. 这是服务器日志的完整转储。 Sorry it's a little messy. 抱歉,这有点混乱。

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 编辑2

I ended up hacking a fix. 我最终破解了补丁。 Rails seems to be able to route the following Rails似乎可以路由以下内容

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

And I put a special flag in params to identify POSTS from the ajax call. 我在参数中放置了一个特殊标志,以从ajax调用中识别POSTS。 Still makes no sense to me as to why rails only wants to route to the RESTful routes.... 对于Rails为什么只想路由到RESTful路由,我仍然没有意义。

I'd try changing your route to something like this: 我会尝试将您的路线更改为以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM