Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 2828

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 26, 20242024-11-26T09:00:09+00:00 2024-11-26T09:00:09+00:00

How to Create an Undo Action with Rails

  • 61k

This article was originally published on Rails Designer.


An undo-action is a common UX paradigm in web-apps. You delete, archive or send something and can then undo that action. Often you see it with a flash/notification component like this one:

Notification component showing Email deleted text + an undo action

(example coming from Rails Designer's NotificationComponent)

After the message was soft-deleted, or put in the “trash”, the notification confirms the action and gives the option to undo.

Some time ago Gmail added the option to “undo” sending an email. Of course, the sending wasn't actually undone, but rather the sending had not yet happened.

Showing Gmail's undo send notification

Other examples could be:

  • Undo Archive;
  • Undo Merge;
  • Undo Mark as Read;
  • Undo Publish;
  • Undo Schedule.

How each action is actually made “undone” is different on the back-end side. Undo Archive might simply remove the archived_at timestamp, similarly with Undo Mark as Read. But Undo Merge might be quite more complicated to pull off.

So let's go over the product bit, that's the same for each action. For this article I've chosen an “Undo Email Delete”.

Undo Action or Confirm Action?

But when to choose an Undo-action or a Confirm Dialog?

  • Undo action: do the action, then give the option to undo it;
  • Confirm action: ask before doing the action.

They both have there place, depending on the action and the consequence. This is the rule-of-thumb I typically use:

Use a Confirm Dialog when the action simply cannot be undone. You actually delete the record from the database or the action is so destructive to one or many rows it's impossible to reverse.

And an Undo action can then be used for non-destructive actions: you soft-delete a record, you add the sending to a background queue with a delay or the action simply sets a timestamp (eg. archived_at).

Next to that, with the examples of deleting or archiving a record, there might not be a need for an undo action. Most users are familiar with these patterns. Also if you rename the action to Move to Bin instead of just Delete they certainly will know where to find the record and move it back.

One last point when building a new feature: I typically would default to the Confirm Dialog's, especially because Rails Designer makes it super easy to add beautiful ones. This is the kind of lean-building I've been practicing for over eight years. Your users will let you know once it annoys them!

Build an Undo Action with Rails

Note: I'm assuming you have access to Rails Designer's UI Components Library.

With that all out of the way, let's build the Undo Email Delete with Rails. Let's first set the stage:

You have a model named Email (app/models/email.rb) that has a deleted_at timestamp. You could also use something like the Discard gem (my preferred choice).

Let's focus on the controller to soft-delete the email.

# app/controllers/emails_controller.rb class EmailsController < ApplicationController   before_action :set_email, only: %w[destroy]   # …   def destroy     @email.update(deleted_at: Time.current)      redirect_to emails_path, notice: "Email deleted successfully."   end    def set_email     @email = Email.find(params[:id])   end   # … end 
Enter fullscreen mode Exit fullscreen mode

Currently it's your standard Rails controller. Onto the turbo stream response.

# app/views/emails/destroy.turbo_stream.erb <⁠%= stream_notification "Your message was deleted", primary_action: {title: "Undo", path: restore_email_path(@email), method: :patch}, time_delay: 10, progress_indicator: "progress_bar" %> 
Enter fullscreen mode Exit fullscreen mode

For this to work, grab the Undoable action variant from the NotificationComponent. If you don't have Rails Designer (get it here!), check out this article about more advanced flash messages in Rails.

If you wonder where stream_notification comes from: it's a helper that's included in Rails Designer.

The astute reader noticed the restore_email_path helper. Let's create the route first and then controller:

# config/routes.rb # … resources :restore_emails, only: [:update] 
Enter fullscreen mode Exit fullscreen mode

# app/controllers/restore_emails_controller.rb class RestoreEmailsController < ApplicationController   def update     @email.update(deleted_at: nil)      redirect_to emails_path, notice: "Email was successfully restored."   end    private    def set_email     @email = Email.find(params[:id])   end end 
Enter fullscreen mode Exit fullscreen mode

Again, your standard Rails controller. Let's create a Turbo Stream response for the update action.

# app/views/emails/update.turbo_stream.erb <⁠%= stream_notification "Moved from the Bin" %> 
Enter fullscreen mode Exit fullscreen mode

Based on your app's set up you can extend this response by injectin the previously-deleted email in the email list. This could be with a Turbo Stream append action (turbo_stream.append("inbox", partial: "emails/email", locals: { email: @email })) or by refreshing if it's a turbo frame (turbo_stream.turbo_frame_reload(#inbox); this is using the turbo_power package).

And that's how simple adding an undo action is with Rails and Turbo. Again: undo action can be an elegant UX, but keep in mind the context it's used for and at what stage your product is.

Anything that's missing or unclear? Let me know below.

hotwirerailsrubywebdev
  • 0 0 Answers
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.