Reputation: 281
I want, on button press, an AJAX call to be sent to my events_controller#check
action. This is the code I have:
events\new.html.erb:
<button id="check-button" type="button">Check</button>
application.js:
$(document).on('click', "#check-button", function(){
var checkList = []; //creates array to store customer ids
...
});
$.ajax({
url: '/events/check',
data:checkList
}
)
});
events_controller#check
def check
checkList = params[:checkList]
...
end
routes.rb
post 'events_controller/check' => 'events#check'
I am getting this error in my console on button click:
Started GET "/events/check?undefined=" for 127.0.0.1 at 2016-02-03 11:36:33 +0000
AbstractController::ActionNotFound (The action 'show' could not be found for EventsController):
actionpack (4.1.8) lib/abstract_controller/base.rb:131:in `process'
actionview (4.1.8) lib/action_view/rendering.rb:30:in `process'
actionpack (4.1.8) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.1.8) lib/action_controller/metal.rb:232:in `block in action'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:82:in `dispatch'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:50:in `call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:73:in `block in call'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `each'
actionpack (4.1.8) lib/action_dispatch/journey/router.rb:59:in `call'
actionpack (4.1.8) lib/action_dispatch/routing/route_set.rb:678:in `call'
...etc
Why is it trying to find the action 'show' and not 'check'? How do I fix this? Thanks
Upvotes: 0
Views: 140
Reputation: 1834
1 Check you have import Jquery file correctly in your page.
<script type = "text/javascript" src = "http://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
$.ajax({
url: '/events/check', // Check URL is correct
cache: false,
success: function(html){
//TO DO Action
}
});
Upvotes: 0
Reputation: 76774
2 problems:
- You've probably got
resources :events
before your declared route (why it's going toshow
)- You've not invoked the
POST
method
You should have the following:
#config/routes.rb
resources :events do
post :check, on: :collection #-> url.com/events/check
end
#app/assets/javascripts/application.js
$(document).on('click', "#check-button", function(){
var checkList = []; //creates array to store customer ids
...
$.ajax({
url: '/events/check',
data: checkList,
method: "POST"
});
});
Upvotes: 1
Reputation: 74738
Seems to me, a post request is expected while type is missing in the request so it makes a GET
request as a default request method.
You need to add type
to your ajax:
type:'post',
Upvotes: 0