All threads / How to create a button with a file upload action

Ask A Question

Notifications

You’re not receiving notifications from this thread.

How to create a button with a file upload action

Nelson Casanova asked in Rails

I've got a form where the user is allowed to import a spreadsheet. Currently the part for the user to select the file looks like:

<%= form_tag import_clearance_crews_path(@clearance), multipart: true, class: "ui form" do %>
  <%= file_field_tag :file, class: "ui button green" %>
  <%= submit_tag "Import", class: "ui button" %>
<% end %>

Which works ok, on clicking 'choose file' the user is allowed to pick up a file and then he needs to click 'import' to finalize the import...

I was wondering if there is a way to just have a button, once you click on it you can pick up a file and straight after selecting it just upload and redirect to X view?

Regards

Hey Nelson,

You should be able to listen to the change event on the file field. That fires anytime the input value changes. Use that to fire off the form submit.

Join the discussion

Want to stay up-to-date with Ruby on Rails?

Join 37,344+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.