简体   繁体   中英

Send filename from JS code to a Rails view

This is my JavaScript code:

var fullPath = document.getElementById('file').value;
if (fullPath) {
   var startIndex = (fullPath.indexOf('\\') >= 0 ? fullPath.lastIndexOf('\\') : fullPath.lastIndexOf('/'));
   var filename = fullPath.substring(startIndex);
   if (filename.indexOf('\\') === 0 || filename.indexOf('/') === 0) {
     filename = filename.substring(1);
   }
}
if(passAll === true){
  alert(filename);
}

As the title sugests, how can I send that var filename to a view history (just print it there) obviously after testing that last if ? My view is empty so it is free for edit. Another question is if the names are going to be saved or discarded after dropping the server?

And here is my controller history_controller.rb :

class HistoryController < ApplicationController
    def historico
    end
end

Try running rails routes in the command line of your project directory. Assuming that HistoryController is in routes.rb , it will show the routes available to send or request data from your controller. It should some something like POST /history history#create . That means you can send your filename via an HTTP POST request {RAILS ROOT URL}/history?filename={THE FILENAME} (if you are developing locally, it will probably be something like http://localhost:3000/history?filename={THE FILENAME} . Then you can access the filename in the create action of your HistoryController with the syntax params[:filename] .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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