Search code examples
ruby-on-railsrubydojojrubyonrails

Rails execute script on server on Dojo Button click


I've ruby script as part of rails application which is used to copy files from some directory to another inside the server. I am using Dojo as front-end. I've a dojo widget which is displaying a button fine. Now I want to the script to be executed when I click the Dojo button.

Wigdet extract:

<input type="button" data-dojo-type="dijit.form.Button" dojoAttachEvent="_onClick:copyfiles" label="Copy Files"></input>

JavaScript function part of widget:

copyfiles: function() {
window.location = '/files/copy_files'
}

in routes I have:

resources :files do
get :copy_files

and in files_controller I have:

def copy_files
  fc = FilesCopier.new
  source = '/home/myname/sourcefiles'
  dest = '/home/myname/backup'
  fc.copy_files(source,dest)
end

But when I clicked the button nothing happens. I've also tried running the code inside a view but nothing happens.


Solution

  • I've resolved it. All I have to do was to change the routes file as per below:

    put :copy_files, :on => :collection
    

    And now even if I put it on show method inside the controller it will work.