Store value of a function in a scaffold

Asked

Viewed 37 times

0

I have a code in Rails with an input that loads files and a button that collects the name of the selected file through input:

<label id="myLabel" for="file"></label>
<%= file_field_tag 'file' %>
<br/>
<br/>
<button onclick="myFunction()">Salvar nome do arquivo</button>

Yes I mixed Html with Ruby code, but I don’t know what a Ruby code label looks like.

This is my JS code, but only the part of the name collection, which is what matters at the moment:

function myFunction() {
  var x = document.getElementById('file').files[0].name; 
  alert(x);
} 

How could I take the value of this function and store it in my scaffold?

My scaffold (which I would like to store it):

<p id="notice"><%= notice %></p>

<h1>Histórico</h1>

<table>
  <thead>
    <tr>
      <th>Arquivos validados:</th>
      <th colspan="3"></th>
    </tr>
  </thead>

  <tbody>
    <% @historicos.each do |historico| %>
      <tr>
        ##ss<td><%= link_to 'Show', historico %></td>
      </tr>
    <% end %>
  </tbody>
</table>
<%= link_to 'Voltar', root_path %>
<br>

Yes, I removed the box to type the texts until why the names would be obtained through my function so I did not see reason to own them I tbm removed the links to the other views(show, delete and Edit).

Thank you in advance!!!

1 answer

0

When you say you want to save in your scaffold, do you mean in the database? Or just display on the page where the file is selected?

If it is for display, you can use Javascript to assign the result of document.getElementById('file').files[0].name; in some element of the DOM. You can do this within this function that you created:

function myFunction() {
  var fileName = document.getElementById('file').files[0].name;
  document.getElementById('#file-name').value = fileName;
}

If it is to save to the database, assuming the file_field_tag is inside a form, you have access to the file data in the controller. In this case, your scaffold page would have access to the instance’s direct data historico.

The two codes are on different pages?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.