2011-01-07 99 views
0

我有這種情況,當我點擊按鈕將調用JavaScript「showpopupfunc()」,然後需要執行ROR menthod「addmcf」 - 是否有可能?如何從Ruby腳本中的ruby on rails調用ROR方法?

<button class="button" id="buttonUpload" onclick="showpopupfunc()" >&nbsp;</button> 

<script> 
     function ajaxFileUpload() 
     { 
      $j.ajaxFileUpload 
      (
       { 
        url:'/ptcgeodatabase', 
        secureuri:false, 
        fileElementId:'fileToUpload', 
        dataType: 'json', 
        success: function (data, status) 
        { 
         if(typeof(data.error) != 'undefined') 
         { 
          if(data.error != '') 
          { 
           alert(data.error); 
          }else 
          { 
           alert(data.msg); 
          } 
         } 
        }, 
        error: function (data, status, e) 
        { 
         alert(e); 
        } 
       } 
      ) 
      return false; 
     } 

     function showpopupfunc() 
     { 
      **window.location="http://127.0.0.1:3006/ptcgeodatabase#addmcf"** 
      showPopWin2('processing', 200, 70, null); 
      setConfirmUnload(false); 


      ajaxFileUpload(); 
     } 
     function addFiles(selectObject, seltext, selvalue) 
     { 
      var optionObject = new Option(seltext,selvalue); 
      var optionRank = document.getElementById("mcffiles").options.length; 
      if(optionRank <= 9) 
      { 
       document.getElementById("mcffiles").options[optionRank]= optionObject; 
      } 
      else 
       alert('Only 9 files can select') 
     } 
     </script> 

控制器:

def addmcf 
# i am doing some operation 
@path=RAILS_ROOT.to_s+"/tmp/upload" 
     @mcfdir=Dir[@path+"/*"] 
     @x='' 
     for i in 0..(@mcfdir.length-1) 
     @[email protected][email protected][i].to_s+'|' 
     end 
end 

回答

2

那麼你可以擊中一個HTTP請求的URL(例如,AJAX請求),其中將在相應的控制器動作執行代碼。

例如,使用默認路由 http://127.0.0.1:3006/ptcgeodatabase/addmcf

應該執行PTCGeoDatabaseController#addcmf

(資本可能不同,結果取決於所定義的路由。)