I've built a Rails 5 RESTful API, and I've been working on a consumer app that I'm using to test out the interface. CR of CRUD works great, but I'm stuck on Delete. Both apps are running locally.
I want to call: DELETE /users/:user_id/attendees/:id(.:format) attendees#destroy
from my external consumer app. Ideally this would be done with conventional Rails view helpers calling the API directly, and wouldn't require custom JavaScript or wrapping the API (or its resources) in the consumer app.
I have it working in 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");
}
});
});
What I'd like is something like:
= link_to "Delete", "http://localhost:3001/users/1/attendees/THE_RESOURCE_ID?user_email=EMAIL&user_token=TOKEN&", method: :delete
which render this 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>
Which doesn't work:
{
"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'"}]}}
I have the rack-cors
gem installed and working.
(I realize there's other refactoring to do, but those are problems I think I know how to solve. :-))
Thanks for your help!
Here's the relevant existing routes for the 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
Sigh...
I was missing remote: true .
= link_to "Delete", "http://localhost:3001/users/1/attendees/THE_RESOURCE_ID?user_email=EMAIL&user_token=TOKEN&", method: :delete, remote: true
For anyone interested this is how I enabled CORS: http://sourcey.com/building-the-prefect-rails-5-api-only-app/#enabling-cors . And this was the SO post the reminded me to use remote: true
: Simple example of Rails 3 + UJS using Ajax to make a remote call, and rendering the resulting JSON object .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.