我的ajaxy电话无效,然后它随机开始工作,然后当我添加更多coffeescript时它停止工作。我删除了添加的coffeescript,但仍然没有工作。
我注意到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吗?
由于
答案 0 :(得分:3)
原来问题在于SlickGrid。无论是SlickGrid还是其中一个必需的jQuery-UI库都存在一些命名问题,并且正在窃取ajax响应的焦点。我决定使用SlickGrid之外的其他东西,所以我从来没有完全诊断出这个问题,但它什么都没做。
注意强>
在Rails 3.2.1中,没有rails.js
,它是jquery_ujs.js
。
感谢您的帮助
答案 1 :(得分:0)
rails.js应该在firebug中可见,如果它不可见,则浏览器不会请求它。您的布局或javascript清单文件必须存在阻止rails.js加载的问题。
在Rails 3.1+中,请确保:
Gemfile 包含:
gem 'jquery-ujs'
(添加并运行bundle
否则)
app / views / layouts / application.html.erb 包含:
<%= javascript_include_tag 'application' %>
app / assets / javascripts / application.js 包含:
//= jquery
//= jquery-ujs
(假设您正在使用资产管道)
如果是这种情况,您应该在Firebug中看到jquery.js
和jquery_ujs.js
,或者在加载页面时看到页面来源。