I am trying to post a form to a Rails controller using jQuery and AJAX however I am getting this error:
Started GET "/subscription_mails/14/create_recipients?utf8=%E2%9C%93&_method=put&authenticity_token=8HtssDY3kxnRlplQbBGlpUspL5ZLq4PqdgiiL5d73yQ%3D&group_ids%5B%5D=3&subscription_mail%5Btarget_users%5D=0&subscription_mail%5Btarget_owners%5D=0&subscription_mail%5Btarget_teachers%5D=0&subscription_mail%5Btarget_teachers%5D=1&subscription_mail%5Btarget_subscribers%5D=0" for 127.0.0.1 at 2012-11-29 19:59:47 +0000
ActionController::RoutingError (No route matches [GET] "/subscription_mails/14/create_recipients"):
actionpack (3.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:21:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/show_exceptions.rb:56:in `call'
railties (3.2.3) lib/rails/rack/logger.rb:26:in `call_app'
railties (3.2.3) lib/rails/rack/logger.rb:16:in `call'
quiet_assets (1.0.1) lib/quiet_assets.rb:20:in `call_with_quiet_assets'
actionpack (3.2.3) lib/action_dispatch/middleware/request_id.rb:22:in `call'
rack (1.4.1) lib/rack/methodoverride.rb:21:in `call'
rack (1.4.1) lib/rack/runtime.rb:17:in `call'
activesupport (3.2.3) lib/active_support/cache/strategy/local_cache.rb:72:in `call'
rack (1.4.1) lib/rack/lock.rb:15:in `call'
actionpack (3.2.3) lib/action_dispatch/middleware/static.rb:62:in `call'
airbrake (3.0.9) lib/airbrake/user_informer.rb:12:in `call'
railties (3.2.3) lib/rails/engine.rb:479:in `call'
railties (3.2.3) lib/rails/application.rb:220:in `call'
railties (3.2.3) lib/rails/railtie/configurable.rb:30:in `method_missing'
rack-block (0.1.1) lib/rack/block.rb:47:in `call'
rack (1.4.1) lib/rack/content_length.rb:14:in `call'
railties (3.2.3) lib/rails/rack/log_tailer.rb:14:in `call'
thin (1.3.1) lib/thin/connection.rb:80:in `block in pre_process'
thin (1.3.1) lib/thin/connection.rb:78:in `catch'
thin (1.3.1) lib/thin/connection.rb:78:in `pre_process'
thin (1.3.1) lib/thin/connection.rb:53:in `process'
thin (1.3.1) lib/thin/connection.rb:38:in `receive_data'
eventmachine (0.12.10) lib/eventmachine.rb:256:in `run_machine'
eventmachine (0.12.10) lib/eventmachine.rb:256:in `run'
thin (1.3.1) lib/thin/backends/base.rb:61:in `start'
thin (1.3.1) lib/thin/server.rb:159:in `start'
rack (1.4.1) lib/rack/handler/thin.rb:13:in `run'
rack (1.4.1) lib/rack/server.rb:265:in `start'
railties (3.2.3) lib/rails/commands/server.rb:70:in `start'
railties (3.2.3) lib/rails/commands.rb:55:in `block in <top (required)>'
railties (3.2.3) lib/rails/commands.rb:50:in `tap'
railties (3.2.3) lib/rails/commands.rb:50:in `<top (required)>'
script/rails:6:in `require'
script/rails:6:in `<main>'
I am not sure why I am getting this error as I have the path defined in my routes. This is my form:
{ :id => “mail-form”, :method => :put } do |f| %>
This is the code in my routes file:
resources :subscription_mails do
put 'create_recipients' => "subscription_mails#create_recipients"
end
And this is my jQuery:
$('#save-config').click(function(){
$.ajax({
url: $('form#mail-form').attr('action'), //sumbits it to the given url of the form
data: $('form#mail-form').serialize(),
dataType: "JSON" // you want a difference between normal and ajax-calls, and json is standard
}).success(function(json){
//act on result.
});
return false; // prevents normal behaviour
});
I’m struggling to figure this out and would really appreciate some help.
Looks like your AJAX request is using the GET verb (default), rather than PUT, which is what you defined in the route. Run
rake -Tto confirm required method and path, then in your AJAX, addtype: 'PUT',in there somewhere.