我的ajaxy調用不起作用,然後它隨機開始工作,然後當我添加更多coffeescript時停止工作。我刪除了添加的咖啡標記,仍然沒有工作。Rails 3 Form Remote不工作
我注意到沒有加載rails.js
(通過firebug/Chrome檢查員進行檢查)。在導軌3.2.1中,rails.js
已被替換爲jquery_ujs.js
?我開始了一個新項目,並注意到rails.js
也沒有。
這裏的形式:
<%= 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 %>
控制檯正顯示出這個當我點擊 「更新視圖」:
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)
所以我知道它的工作。如果我刪除對remote: true
的呼叫,我會將預期結果作爲json對象。
這裏是我的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'
是否有一個有錯字或東西嗎?應該在firebug中看到rails.js嗎?
感謝
已rails.js被替換'jquery_ujs.js'?我開始了新的項目,並且'rails.js'也沒有。 – 2012-03-26 16:28:58
rails.js駐留在jquery-ujs項目中,請參閱[jquery-ujs on github](https://github.com/rails/jquery-ujs/tree/master/src)。您的應用程序中的文件將是rails.js。如果它尚不存在,請將jquery-rails添加到您的Gemfile中以獲取它。您需要確保rails.js通過javascript_include_tag或通過您的application.js清單文件包含在您的模板中。 – 2012-03-26 19:24:09
好吧,這可能不正確,請檢查[jquery-ujs回購](https://github.com/rails/jquery-ujs)上自述文件的安裝部分。在Rails 3.1中,它提到了加入: // =需要jquery // =要求jquery_ujs 到您的清單 – 2012-03-26 19:28:54