---
title: "Rails 6 adds ActiveSupport::ActionableError"
description: "Rails 6 adds support for dispatching actions from error page"
canonical_url: "https://www.bigbinary.com/blog/rails-6-adds-active-support-actionable-error"
markdown_url: "https://www.bigbinary.com/blog/rails-6-adds-active-support-actionable-error.md"
---

# Rails 6 adds ActiveSupport::ActionableError

Rails 6 adds support for dispatching actions from error page

- Author: Taha Husain
- Published: October 1, 2019
- Categories: Rails 6, Rails

When working in a team on a Rails application, we often bump into
`PendingMigrationError` or other errors that need us to run a rails command,
rake task etc.

Rails introduced a way to resolve such frequent errors in development from error
page itself.

Rails 6 added
[`ActiveSupport::ActionableError`](https://github.com/rails/rails/pull/34788)
module to define actions we want perform on errors, right from the error page.

For example, this is how `PendingMigrationError` page looks like in Rails 6.

![How Actionable error looks like in Rails 6](https://www.bigbinary.com/blog/images/images_used_in_blog/2019/rails-6-adds-active-support-actionable-error/rails-6.png)

By default, a button is added on error screen that says _Run pending
migrations_. Clicking on this button would dispatch `rails db:migrate` action.
Page will reload once migrations run successfully.

We can also define custom actions to execute on errors.

### How to define actions on error?

We need to include `ActiveSupport::ActionableError` module in our error class.
We can monkey patch an existing error class or define custom error class.

`#action` api is provided to define actions on error. First argument in
`#action` is name of the action. This string would be displayed on the button on
error page. Second argument is a block where we can write commands or code to
fix the error.

Let's take an example of seeding posts data from controller, if posts not
already present.

```ruby

# app/controllers/posts_controller.rb

class PostsController < ApplicationController

def index
@posts = Post.all
if @posts.empty?
raise PostsMissingError
end
end

end
```

```ruby

# app/errors/posts_missing_error.rb

class PostsMissingError < StandardError

include ActiveSupport::ActionableError

action "seed posts data" do
Rails::Command.invoke 'posts:seed'
end

end

```

```ruby

# lib/tasks/posts.rake

namespace :posts do

desc 'posts seed task'
task :seed do
Post.create(title: 'First Post')
end

end
```

```ruby

# app/views/posts/index.html.erb

<% @posts.each do |post| %>
<%= post.title %>
<% end %>
```

Let's check `/posts` (`posts#index` action) when no posts are present. We would
get an error page with an action button on it as shown below.

![Actionable error - seed posts data](https://www.bigbinary.com/blog/images/images_used_in_blog/2019/rails-6-adds-active-support-actionable-error/posts-missing-error.png)

Clicking on _seed posts data_ action button will run our rake task and create
posts. Rails will automatically reload `/posts` after running rake task.

![Posts index page](https://www.bigbinary.com/blog/images/images_used_in_blog/2019/rails-6-adds-active-support-actionable-error/posts-index.png)

[`ActionDispatch::ActionableExceptions`](https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/actionable_exceptions.rb)
middleware takes care of invoking actions from error page.
`ActionableExceptions` middleware dispatches action to `ActionableError` and
redirects back when action block has successfully run. Action buttons are added
on error page from
[this middleware template](https://github.com/rails/rails/blob/master/actionpack/lib/action_dispatch/middleware/templates/rescues/_actions.html.erb).

Checkout the [pull request](https://github.com/rails/rails/pull/34788) for more
information on actionable error.

## Links

- [Human page](https://www.bigbinary.com/blog/rails-6-adds-active-support-actionable-error)
