Why am I not able to execute AJAX through rails while going through controller index action?

This is my first time trying AJAX request on Ruby on Rails. I followed the RoR guide to come up with this script. My code is as follows.

course_controller

def index
     @course = Course.all
     respond_to do |format| 
          format.html{}
          format.js {}
     end
end

views/courses/index.js.erb

$('#dummy_div').hide();

views/courses/trial.html.erb (view from where I am calling the AJAX / index controller)

<body>
<%= form_for(Course.new, :method => :get, remote: true) do |f| %>
<%= submit_tag "Search", :name => 'course_list' %>
<%end%>

<div id = "dummy_div" style = "width:100px; height: 100px;background-color:black;"></div>
</body>

My expected result is that the div should disappear once I have submitted the Search function. However, this does not happen. I have not put in embedded ruby because I am just testing whether plain JS works in the first place. My intention is to put in erb later (just to be clear that I am testing this for AJAX). Why does the JS in the index.js.erb file not get executed?

Additionally, how do I debug AJAX requests in Ruby on Rails?

Answers


What I have done above was absolutely correct and should be the correct way to route to the js file. This was actually an issue with initializing the model object course and had nothing to do with the code above (I had a small typo mismatch with the model name - Course). A better way to test the ajax than making the div disappear is using the console.log("i'm at: views/courses/index.js.erb"); or alert("i'm at: views/courses/index.js.erb"); as mentioned by John in the comment above.


Need Your Help

Oracle Stored Procedure with Alter command

sql oracle stored-procedures plsql

I am trying to build an Oracle stored procedure which will accept a table name as a parameter. The procedure will then rebuild all indexes on the table.

exclude a folder from expressjs routing

javascript node.js backbone.js express

On my expressjs app, public assets are loaded in this way: