Skip to content Skip to sidebar Skip to footer

Ruby On Rails Jquery Not Calling The Right Controller Action

I read all the answers that popped up when I started posting this question but they don't seem to solve my issue. I added a new action called get_results to my controller (in addit

Solution 1:

Try this:

Your javascript should be

<scripttype="text/JavaScript">
    $(function(){
         $('.menu_class').bind('change', function(){
             $.ajax({
               url: "<%= get_results_my_tests_url %>",
               data: {
                   param_one: $(this).val()
               }
             });
         });
     });
</script>

Your routes:

resources :my_testsdo# if "/my_tests/get_results" is really what you want
    collection do
        get :get_resultsend#if "/my_tests/1/get_results" is what you actually want#it would make more sense, especially because you have @my_test in the controller
    member do
        get :get_resultsendend

Solution 2:

$.ajax('#{:controller => "my_tests", :action => "get_results"}?param_one=' + $(this).val());

Since there is no ERB in that snippet, the above will just be a normal string in JS. It will construct a url like:

http://example.com/the_current_page#{:controller => "my_tests", :action => ....

and the part of the URL after the # won't get sent to the server. Take a look at it in the network panel and it should be clear what's happening.

Post a Comment for "Ruby On Rails Jquery Not Calling The Right Controller Action"