Comment afficher le contenu du formulaire qui vient d'être entré dans l'option «: confirm =>» sur Ruby on Rails

StackOverflow https://stackoverflow.com/questions/1879344

  •  18-09-2019
  •  | 
  •  

Question

J'essaie d'avoir un moyen de confirmer les informations saisies avant de les enregistrer dans la base de données

A envisagé de faire une page de confirmation individuelle comme discuté ici

Ruby on Rails: Page de confirmation pour la création d'objets ActiveRecord

Cependant, mon formulaire comprend un fichier joint à l'aide de Paperclip, ce qui en fait plus un problème à implémenter.

J'ai pensé à avoir un: confirm => pop up qui montrerait les informations que l'utilisateur venait d'entrer.

Le problème est de savoir comment montrer les informations que l'utilisateur venait d'entrer, par exemple

<% form_for @entry, :html => { :multipart => true } do |f| %>
  <%= f.error_messages %>


    <%= f.label :name %><br />
    <%= f.text_field :name %>

    <%= f.label :file %><br />
    <%= f.file_field :file %>

    <%= f.submit 'Create', :confirm => "????? " %>

<% end %>
Était-ce utile?

La solution

Given that your loading attachments it may not be a bad idea to render a staging view including information derived from the attachment allowing the user to confirm. As in display the file if it's an image, or the first paragraph of text if it's a text file, etc.

It's going to take more work than the just adding a confirm pop up, but I feel the enhanced user experience is worth the extra effort.

I'm not familiar with the way that paperclip works. So you're mostly on your own for the intimate details.

You will probably have to create a record before the staging view can be rendered with the sample of the uploaded file. To accomplish that I'd set up an "active" column on the model in question, that defaults to false.

Usage would look something like this:

  1. User complets new form.
  2. Attachment is updated and records are saved, with the active field set to false.
  3. Redirected to confirmation page that is essentially the show page with a confirm link/button and a cancel link/button
  4. a. When the confirm link/button is clicked it sends a request to the controller triggering the update action on this record setting active to true.

    b. When the cancel link/button is clicked it sends a request to the controller trigering the destroy action on this record.

All that's left is to set up a recurring task to remove objects that are inactive and were crated long enough ago that it's safe to assume the user has just ended the browser session.

Autres conseils

The confirm option for the Rails submit method can only take a text value.

If you want to dynamically generate the confirm text, one way you could do it is to write your own HTML submit tag, and write custom javascript to analyse the fields you want to use in your generated text.

Or you could use the Rails submit method, but use something like JQuery to add an event handler to it.

I'd just throw my js into an onclick handler. That's all Rails does.

<%= f.submit 'Create', :onclick => "confirm('Name is ' + $F('entry_name'));" %>

(note, didn't test that, but it looks close. confirm is a core js function, not part of any lib)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top