Ajax不适用于嵌套循环

时间:2016-08-24 10:28:26

标签: javascript ruby-on-rails-4

在sizes.rb控制器

def show
   @size = Size.find(params[:id])
end

show.html.erb

    <% @sizes.each do |size| %>
      <div class="col-md-8">
        <table class="table table-striped table-bordered table-hover">
         <thead>
          <tr>
           <th>Custom Size Name</th>
          </tr>
         </thead>
         <% size.custom_sizes.each do |custom_size| %>
           <tbody>
             <tr>
               <td>
                 <%= link_to "Portrait",portrait_indesign_path(size.id,:id1=>custom_size.id,:portrait=>"portrait"),:remote=>true,:id=>"portrait1", :class => "btn btn-default btn-xs" %>  |
                 <%= link_to "Landscape",portrait_indesign_path(size.id,:id1=>custom_size.id,:portrait=>"landscape"),:remote=>true,:id=>"portrait2",:class => "btn btn-default btn-xs"%>
               </td>
             </tr>
           </tbody>
         <% end%>
      </table>
      <div id="modal-box" style="display:none;"></div>
    </div>
  <% end%>

在sizes.rb控制器

def portrait_indesign
    custom_size=CustomSize.find(params[:id1])
    size=Size.find(params[:id])
    @size1=size.no_of_columns        
    respond_to do |format|
      if params[:portrait] == "portrait"
        format.js {render :action => "portrait_indesign"}
      elsif params[:portrait] == "landscape"
        format.js {render :action => "landscape_indesign"}
      end
    end
  end

在portrait_indesign.js

$("#modal-box").replaceWith("<%= escape_javascript(render :partial => "sizes/portrait_indesign") %>");

在_portrait_indesign.html.erb

 <html>
      <body>
        <a id="download_file" href="."><button type="submit" value="Ok" id="ok" class = "btn btn-default btn-xs">Download Portrait Template - <%= @sizeName%></button></a>  
      </body>
    </html>
    <script>
      '<%=@size1%>'
      var txt = document.getElementById('download_file');      
      document.getElementById("download_file").setAttribute("href", "");
      txt.value ='var myDocument = app.documents.add();pageHeight = "800pt";pageWidth = "600pt";myDocument.viewPreferences.horizontalMeasurementUnits = MeasurementUnits.points;myDocument.viewPreferences.verticalMeasurementUnits = MeasurementUnits.points;myDocument.viewPreferences.rulerOrigin = RulerOrigin.pageOrigin;var myMasterSpread = myDocument.masterSpreads.item(0); var myMarginPreferences = myMasterSpread.pages.item(0).marginPreferences;myMarginPreferences.columnCount = '+'<%=@size1%>'+';myMarginPreferences.columnGutter = 14;';
document.getElementById('download_file').onclick = function(code) {            document.getElementById("download_file").setAttribute("download", '<%=@sizeName%>'+"_portrait.jsx");
            this.href = 'data:text/x-csrc;charset=utf-8,'
              + encodeURIComponent(txt.value);
              window.location.reload(true);
        };
</script>

像portrait_indesign.js和_portrait_indesign.html.erb一样,我写了风景画。 Portrait and Landscape buttons are present in this image

当我点击纵向按钮时,下载按钮将会出现在我可以下载相应代码的地方。点击下载按钮之前如果我点击了横向按钮,但横向下载按钮没有出现。仍然只是在纵向下载按钮。刷新页面后它正常工作。 portrait and landscape

0 个答案:

没有答案