开发者

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 ?

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

For example, with the default routing http://127.0.0.1:3006/ptcgeodatabase/addmcf

should execute PTCGeoDatabaseController#addcmf

(Capitalisation may differ and result depends on the routes that are defined.)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜