My ajaxy calls weren’t working, then it randomly started working, then it stopped working when I added more coffeescript. I removed the added coffeescript and still, no worky.
I noticed that rails.js isn’t being loaded (via inspection by firebug/Chrome inspector). Has rails.js been replaced by jquery_ujs.js in rails 3.2.1? I started a new project and noticed that rails.js wasn’t there either.
Here’s the form:
<%= form_tag("#{tybleau_api_path}.json", remote: true, id: "sportForm", :'data-type' => 'json' ) do%>
<%= select_tag("state", options_from_collection_for_select(@states, "HometownState", "HometownState"), prompt: "Choose a State", class: "submittable") %>
<%= submit_tag "Update View" %>
<% end %>
The console is showing this when I click “Update View”:
Started POST "/tybleau/api.json" for 127.0.0.1 at 2012-03-22 17:40:03 -0700
Processing by TybleauController#api as JSON
Parameters: {"utf8"=>"✓", "authenticity_token"=>"I6752Fit6cCIuASMr4/FD+yLTEBrS2oPhnNBbJQE67k=", "state"=>"AZ", "commit"=>"Update View"}
Player Load (745.1ms) SELECT FirstName FROM `players` INNER JOIN `players_to_teams` ON `players_to_teams`.`player_id` = `players`.`id` INNER JOIN `teams` ON `teams`.`id` = `players_to_teams`.`team_id` WHERE `teams`.`SportTypeId` = 2 AND `players`.`hometownstate` = 'AZ'
EXPLAIN (0.4ms) EXPLAIN SELECT FirstName FROM `players` INNER JOIN `players_to_teams` ON `players_to_teams`.`player_id` = `players`.`id` INNER JOIN `teams` ON `teams`.`id` = `players_to_teams`.`team_id` WHERE `teams`.`SportTypeId` = 2 AND `players`.`hometownstate` = 'AZ'
EXPLAIN for: SELECT FirstName FROM `players` INNER JOIN `players_to_teams` ON `players_to_teams`.`player_id` = `players`.`id` INNER JOIN `teams` ON `teams`.`id` = `players_to_teams`.`team_id` WHERE `teams`.`SportTypeId` = 2 AND `players`.`hometownstate` = 'AZ'
+----+-------------+------------------+--------+---------------+---------+---------+----------------------------------------------+--------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+------------------+--------+---------------+---------+---------+----------------------------------------------+--------+-------------+
| 1 | SIMPLE | players_to_teams | ALL | NULL | NULL | NULL | NULL | 260239 | |
| 1 | SIMPLE | players | eq_ref | PRIMARY | PRIMARY | 4 | mysql_development.players_to_teams.player_id | 1 | Using where |
| 1 | SIMPLE | teams | eq_ref | PRIMARY | PRIMARY | 4 | mysql_development.players_to_teams.team_id | 1 | Using where |
+----+-------------+------------------+--------+---------------+---------+---------+----------------------------------------------+--------+-------------+
3 rows in set (0.00 sec)
Completed 200 OK in 1034ms (Views: 154.7ms | ActiveRecord: 825.9ms)
So I know it’s working. If I remove the call to remote: true, I get the expected result as a json object.
Here’s my coffeescript:
jQuery ->
$("#sportForm")
.bind 'ajax:beforeSend', (xhr, settings) ->
alert 'loading'
.bind 'ajax:success', (data, status, xhr) ->
alert 'success'
.bind 'ajac:complete', (xhr, status) ->
alert 'success'
.bind 'ajax:error', (xhr, status, error) ->
alert 'error'
Is there a typo or something in there? Should rails.js be viewable in firebug?
Thanks
Turns out the problem was with SlickGrid. Either SlickGrid or one of the required jQuery-UI libraries was having some naming problem and was stealing the focus of the ajax responses. I’ve decided to use something other than SlickGrid, so I never fully diagnosed the problem, but it’s working nothing.
Note
In Rails 3.2.1, there is no
rails.js, it’sjquery_ujs.js.Thanks for the help