I'm working in a simple increment fonction who is not working. In my console I have this error "NetworkError: 404 Not Found - ..../test/increment_nbr" and I'm not able to fix it.
Html:
<button id="incr">AJAX2</button>
<script>
$("#incr").on("click", function(e) {
e.preventDefault();
$.ajax({
url: "/test/increment_nbr",
dataType: "json",
type: "POST",
success: function(data) {
}
});
})
</script>
My controller:
respond_to :html, :json
def increment_nbr
@user = User.find(params[:id])
@user.increment! :nbr_set
render json: { nbr: @user.nbr_set }.to_json
end
Routes:
resources :test do
member do
put :increment_nbr
post :increment_nbr
end
end
Your routing is incorrect - you've defined your two increment_nbr
routes as member routes, meaning they operate on an instance of your parent test
route and would generate URLs like /test/2/increment_nbr
.
See http://guides.rubyonrails.org/routing.html#adding-more-restful-actions for more information