开发者

Ajax POST to custom action in rails

开发者 https://www.devze.com 2023-02-22 13:16 出处:网络
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:

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:

$("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:

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

which corresponds to this controller method in MeetingsController:

def user_left
  #do stuff
end

However, when I execute the AJAX post method I get the following error:

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

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_re开发者_Go百科cord/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

I ended up hacking a fix. Rails seems to be able to route the following

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

And I put a special flag in params to identify POSTS from the ajax call. Still makes no sense to me as to why rails only wants to route to the RESTful routes....


I'd try changing your route to something like this:

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

精彩评论

暂无评论...
验证码 换一张
取 消