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

Reply

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.

Reply
Join the discussion
Create an account Log in

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

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

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