1

我一直在从 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&#47;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)
4

2 回答 2

0

问题是我缺少“数据类型”=>“json”。这使得控制器能够将其检测为 json 调用(具有适当的内容类型)并因此做出相应的响应:

<%= link_to image_tag("review-icon2_16x16.png", :border=>0),
                      {:controller=>"sponjson", :action=>"review", :id=>"#{sponsor.id}"},
                      {:remote=>true, "data-type"=>"json", :id=>"review_spon_id_#{sponsor.id}", :alt=>"Flag for Review", :title=>"Flag for Review", :confirm => "Review ...  Are you sure?" }
                      %><br/>
于 2013-12-01T20:48:09.477 回答
0

看起来您将 JS 事件附加到 data-id 而不是链接的 id。您还在图像上设置了不支持的边框(此处列出了支持的参数)。

尝试将您的链接更改为以下内容:

<%= link_to review_sponjson(sponsor), remote: true, confirm: "Are you sure?", id: "review_spon_id_#{sponsor.id}" do %>
  <%= image_tag("review-icon2_16x16.png", alt: "Flag for Review") %>
<% end %>
于 2013-10-20T04:42:59.127 回答