Ajax POST 到 Rails 中的自定义操作
我有以下 JQuery 函数,它向服务器发送一个 post 请求,以在有人单击 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');
}
});
});
在我的 paths.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'
编辑2
我最终修复了这个问题。 Rails 似乎能够路由以下内容
match 'meetings' => 'meetings#index', :via => :post
,我在 params 中放置了一个特殊标志来识别来自 ajax 调用的 POSTS。对于为什么 Rails 只想路由到 RESTful 路线对我来说仍然没有意义......
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_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
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....
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会尝试将您的路线更改为如下所示:
I'd try changing your route to something like this: