我一直在从 Rails 2.3.14 迁移到 Rails 3.0.20,但我不知道为什么我不能让 Ajax 调用通过带有 :remote=>true 的 link_to 工作。
宝石文件
gem "jquery-rails", "~> 1.0.19"
命令行:
rails generate jquery:install --ui
remove public/javascripts/prototype.js
remove public/javascripts/effects.js
remove public/javascripts/dragdrop.js
remove public/javascripts/controls.js
copying jQuery (1.7.1)
identical public/javascripts/jquery.js
identical public/javascripts/jquery.min.js
copying jQuery UI (1.8.16)
identical public/javascripts/jquery-ui.js
identical public/javascripts/jquery-ui.min.js
copying jQuery UJS adapter (822920)
remove public/javascripts/rails.js
identical public/javascripts/jquery_ujs.js
在头部:
<%= csrf_meta_tag %>
<%= javascript_include_tag :defaults %>
结果是:
<meta name="csrf-token" content="qH17FbbfwQNFTHWte/CBYopzfEvf5gnHxGEUdDBfV+Y="/>
<script src="/javascripts/jquery.js?1381981377" type="text/javascript"></script>
<script src="/javascripts/jquery-ui.js?1381981377" type="text/javascript"></script>
<script src="/javascripts/jquery_ujs.js?1381981377" type="text/javascript"></script>
<script src="/javascripts/application.js?1381984319" type="text/javascript"></script>
链接标签不起作用:
<%= link_to image_tag("review-icon2_16x16.png", :border=>0),
{:controller=>"sponjson", :action=>"review", :id=>"#{sponsor.id}"},
{:remote=>true, :id=>"review_spon_id_#{sponsor.id}", :alt=>"Flag for Review", :title=>"Flag for Review", :confirm => "Review ... Are you sure?" }
%><br/>
编辑。添加链接标签扩展 HTML:
<a href="/sponjson/review/77" alt="Flag for Review" data-confirm="Review .. Are you sure?" data-remote="true" id="review_spon_id_77" title="Flag for Review"><img alt="Review-icon2_16x16" border="0" src="/images/review-icon2_16x16.png?1338407096" /></a>
支持UJS:
<%= raw("<script type='text/javascript'>
$('#review_spon_id_#{sponsor.id}').bind('ajax:before', function(evt, status, data, xhr) {
beforeAjaxStatus('sstatus_#{sponsor.id}')
});
$('#review_spon_id_#{sponsor.id}').bind('ajax:complete', function(evt, status, data, xhr) {
completeAjaxStatusReview('sstatus_#{sponsor.id}')
});
</script>" )%>
问题:当我单击链接时,它不是 AJAX 请求。用这个响应调用控制器就好了:
respond_to do |format|
format.html { (head :ok) }
format.js { (head :ok) }
format.xml { (head :ok) }
end
编辑:路线:
/sponjson/review/:id(.:format) {:controller=>"sponsors", :action=>"review"}
/sponjson/:id(.:format) {:controller=>"sponsors", :action=>"update"}
/:controller(/:action(/:id))(.:format)