简体   繁体   English

如何从ruby on rails中的java脚本调用ROR方法?

[英]how to call the ROR method from java script in ruby on rails?

i have the scenario when i am clicking button will invoke javascript "showpopupfunc()" and then need to execute the ROR menthod "addmcf" - Is it possible ? 我有一种情况,当我单击按钮时将调用javascript“ showpopupfunc()”,然后需要执行ROR方法“ addmcf”-有可能吗?

<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>

Controller: 控制器:

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)
        @x=@x+@mcfdir[i].to_s+'|'
      end
end

Well you can hit a URL with a HTTP request (for example, an AJAX request) which will execute the code in the corresponding controller action. 好吧,您可以使用HTTP请求(例如AJAX请求)访问URL,该请求将在相应的控制器操作中执行代码。

For example, with the default routing http://127.0.0.1:3006/ptcgeodatabase/addmcf 例如,使用默认路由http://127.0.0.1:3006/ptcgeodatabase/addmcf

should execute PTCGeoDatabaseController#addcmf 应该执行PTCGeoDatabaseController#addcmf

(Capitalisation may differ and result depends on the routes that are defined.) (大小写可能有所不同,结果取决于所定义的路线。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM