I am pretty new to rails, please help me out.
This is my javascript i want filename
value to be passed in the controller page_controller
to the create_table
action
$(document).ready(function(){
$("#prefix, #client, #myFile").change(function(){
concatenated_string = $("#prefix").val() + "_" + $("#client").val() + "_" + $("#myFile").val();
$("#concatenated_string").val(concatenated_string);
myvar = concatenated_string.replace(/C:\\fakepath\\/i, '');
myvar = myvar.replace(/\..+$/, '');
myvar = myvar.replace(/\s+/, "_");
var filename = myvar //This variable
$("#temp_display").text(filename)
})
})
This below file is my page_controller.rb
and the create_table
action
class PageController < ApplicationController
# before_action :authenticate_user!
#before_action :set_company, only: [:show, :edit, :update, :destroy]
before_action :authenticate_user!, only: [:index]
def home
end
def titlebar
end
def insert_table
#redirect_to :action => "create_table"
end
def create_table
filename = params[:filename]
end
end
This is what am getting in terminal
Started POST "/page/create_table" for ::1 at 2017-01-28 20:45:05 +0530
Processing by PageController#create_table as HTML
Parameters: {"utf8"=>"✓", "authenticity_token"=>"TAcnZfhr8Amg8/xupgJVlAosz+/hNPgFgxVT4cg5x+OKvRwvrtm6Z5momvQiHsYBcl1tZWUu525Z1VTxqWIu8Q==", "commit"=>"upload file"}
Rendering page/create_table.html.erb within layouts/application
Rendered page/create_table.html.erb within layouts/application (0.5ms)
Completed 200 OK in 190ms (Views: 186.7ms | ActiveRecord: 0.0ms)
You can have a hidden input field in the form like below for which the filename value can be appended, so that you can capture the value in the create_table
action.
#in your form
<input type="hidden" name="filename" id="my_file_name" value="">
And in script
$(document).ready(function(){
$("#prefix, #client, #myFile").change(function(){
concatenated_string = $("#prefix").val() + "_" + $("#client").val() + "_" + $("#myFile").val();
$("#concatenated_string").val(concatenated_string);
myvar = concatenated_string.replace(/C:\\fakepath\\/i, '');
myvar = myvar.replace(/\..+$/, '');
myvar = myvar.replace(/\s+/, "_");
var filename = myvar //This variable
$("#temp_display").text(filename);
$("#my_file_name").val() = filename; //Add this line
})
})
Now, you can capture the value of filename with params[:filename]
.
I got the solution .
this is my js
function myFunction()
{
concatenated_string = $("#prefix").val() + "_" + $("#client").val() + "_" + $("#myFile").val();
$("#concatenated_string").val(concatenated_string);
myvar = concatenated_string.replace(/C:\\fakepath\\/i, '');
myvar = myvar.replace(/\..+$/, '');
myvar = myvar.replace(/\s+/, "_");
var filename = myvar;
$("#temp_display").text(filename);
document.getElementById("my_file_name").value = filename;
}
this is in my view as suggested by pavan
<input type="hidden" name="filename" id="my_file_name" value="">
this is my controller
def create_table
filename = params[:filename]
content = params[:file].path
end
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.