我已经构建了一个Rails 5 RESTful API,我一直在研究一个消费者应用程序,我用它来测试界面。 CRUD的CR效果很好,但我坚持删除。这两个应用都在本地运行。
我想打电话:DELETE /users/:user_id/attendees/:id(.:format) attendees#destroy
从我的外部消费者应用程序理想情况下,这可以通过直接调用API的传统Rails视图助手来完成,并且不需要自定义JavaScript或将API(或其资源)包装在消费者应用程序中。
我在JavaScript中使用它:
$("#button").click(function(){
resource_id = $("#button").data('resource-id');
$.ajax({
type: "DELETE",
url: "http://localhost:3001/users/1/attendees/" + resource_id + "?user_email=EMAIL&user_token=TOKEN&",
dataType: "json",
data: {"_method":"delete"},
complete: function(){
alert("Deleted successfully");
}
});
});
我想要的是:
= link_to "Delete", "http://localhost:3001/users/1/attendees/THE_RESOURCE_ID?user_email=EMAIL&user_token=TOKEN&", method: :delete
它呈现这个HTML:
<a rel="nofollow" data-method="delete" href="http://localhost:3001/users/1/attendees/THE_RESOURCE_ID?user_email=EMAIL&user_token=TOKEN&">Delete</a>
哪个不起作用:
{
"status":404,
"error":"Not Found",
"exception":"#\u003cActionController::RoutingError: No route matches [POST] \"/users/1/attendees/2\"\u003e",
"traces":{
"Application Trace":[],
"Framework Trace":
[{"id":0,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'"},
{"id":1,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'"},
{"id":2,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/railties/lib/rails/rack/logger.rb:36:in `call_app'"},
{"id":3,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/railties/lib/rails/rack/logger.rb:24:in `block in call'"},
{"id":4,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/activesupport/lib/active_support/tagged_logging.rb:70:in `block in tagged'"},
{"id":5,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/activesupport/lib/active_support/tagged_logging.rb:26:in `tagged'"},
{"id":6,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/activesupport/lib/active_support/tagged_logging.rb:70:in `tagged'"},
{"id":7,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/railties/lib/rails/rack/logger.rb:24:in `call'"},
{"id":8,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/actionpack/lib/action_dispatch/middleware/request_id.rb:24:in `call'"},
{"id":9,"trace":"rack (2.0.0.alpha) lib/rack/runtime.rb:22:in `call'"},
{"id":10,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/activesupport/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'"},
{"id":11,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/actionpack/lib/action_dispatch/middleware/executor.rb:12:in `call'"},
{"id":12,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/actionpack/lib/action_dispatch/middleware/static.rb:136:in `call'"},
{"id":13,"trace":"rack (2.0.0.alpha) lib/rack/sendfile.rb:111:in `call'"},
{"id":14,"trace":"rack-cors (0.4.0) lib/rack/cors.rb:80:in `call'"},
{"id":15,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/railties/lib/rails/engine.rb:522:in `call'"},
{"id":16,"trace":"puma (3.2.0) lib/puma/configuration.rb:227:in `call'"},
{"id":17,"trace":"puma (3.2.0) lib/puma/server.rb:561:in `handle_request'"},
{"id":18,"trace":"puma (3.2.0) lib/puma/server.rb:406:in `process_client'"},
{"id":19,"trace":"puma (3.2.0) lib/puma/server.rb:271:in `block in run'"},
{"id":20,"trace":"puma (3.2.0) lib/puma/thread_pool.rb:111:in `call'"},
{"id":21,"trace":"puma (3.2.0) lib/puma/thread_pool.rb:111:in `block in spawn_thread'"}],
"Full Trace":[{"id":0,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/actionpack/lib/action_dispatch/middleware/debug_exceptions.rb:53:in `call'"},
{"id":1,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/actionpack/lib/action_dispatch/middleware/show_exceptions.rb:31:in `call'"},
{"id":2,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/railties/lib/rails/rack/logger.rb:36:in `call_app'"},
{"id":3,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/railties/lib/rails/rack/logger.rb:24:in `block in call'"},
{"id":4,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/activesupport/lib/active_support/tagged_logging.rb:70:in `block in tagged'"},
{"id":5,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/activesupport/lib/active_support/tagged_logging.rb:26:in `tagged'"},
{"id":6,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/activesupport/lib/active_support/tagged_logging.rb:70:in `tagged'"},
{"id":7,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/railties/lib/rails/rack/logger.rb:24:in `call'"},
{"id":8,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/actionpack/lib/action_dispatch/middleware/request_id.rb:24:in `call'"},
{"id":9,"trace":"rack (2.0.0.alpha) lib/rack/runtime.rb:22:in `call'"},
{"id":10,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/activesupport/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'"},
{"id":11,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/actionpack/lib/action_dispatch/middleware/executor.rb:12:in `call'"},
{"id":12,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/actionpack/lib/action_dispatch/middleware/static.rb:136:in `call'"},
{"id":13,"trace":"rack (2.0.0.alpha) lib/rack/sendfile.rb:111:in `call'"},
{"id":14,"trace":"rack-cors (0.4.0) lib/rack/cors.rb:80:in `call'"},
{"id":15,"trace":"/Users/MYUSER/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/bundler/gems/rails-7b82e1c77b48/railties/lib/rails/engine.rb:522:in `call'"},
{"id":16,"trace":"puma (3.2.0) lib/puma/configuration.rb:227:in `call'"},
{"id":17,"trace":"puma (3.2.0) lib/puma/server.rb:561:in `handle_request'"},
{"id":18,"trace":"puma (3.2.0) lib/puma/server.rb:406:in `process_client'"},
{"id":19,"trace":"puma (3.2.0) lib/puma/server.rb:271:in `block in run'"},
{"id":20,"trace":"puma (3.2.0) lib/puma/thread_pool.rb:111:in `call'"},
{"id":21,"trace":"puma (3.2.0) lib/puma/thread_pool.rb:111:in `block in spawn_thread'"}]}}
我安装了rack-cors
gem并正在使用。
(我知道还有其他的重构要做,但那些是我认为我知道如何解决的问题。: - ))
感谢您的帮助!
以下是API的相关现有路线:
user_attendees GET /users/:user_id/attendees(.:format) attendees#index
POST /users/:user_id/attendees(.:format) attendees#create
user_attendee GET /users/:user_id/attendees/:id(.:format) attendees#show
PATCH /users/:user_id/attendees/:id(.:format) attendees#update
PUT /users/:user_id/attendees/:id(.:format) attendees#update
DELETE /users/:user_id/attendees/:id(.:format) attendees#destroy
答案 0 :(得分:1)
唉...
我错过了 remote:true 。
= link_to "Delete", "http://localhost:3001/users/1/attendees/THE_RESOURCE_ID?user_email=EMAIL&user_token=TOKEN&", method: :delete, remote: true
对于任何感兴趣的人,这是我启用CORS的方式:http://sourcey.com/building-the-prefect-rails-5-api-only-app/#enabling-cors。这是SO帖子,提醒我使用remote: true
:Simple example of Rails 3 + UJS using Ajax to make a remote call, and rendering the resulting JSON object。