What is a Smart Action?
Sooner or later, you will need to perform actions on your data that are specific to your business. Moderating comments, generating an invoice, logging into a customer’s account or banning a user are exactly the kind of important tasks to unlock in order to manage your day-to-day operations. On our Live Demo example, ourcompanies collection has many examples of Smart Action. The simplest one is Mark as live.
If you’re looking for information on native actions (CRUD), check out this page.
Creating a Smart action
In order to create a Smart action, you will first need to declare it in your code for a specific collection. Here we declare a Mark as Live Smart action for thecompanies collection.
- Rails
- Django
At this point, the Smart Action does nothing, because no route in your Admin backend handles the API call yet.
live.
- Rails
- Django
- Laravel
The route declaration takes place in The business logic in this Smart Action is extremely simple. We only update here the attribute
config/routes.rb.status of the companies to the value live:You may have to add CORS headers to enable the domain
app.forestadmin.com to trigger API call on your Application URL, which is on a different domain name (e.g. localhost:3000).What’s happening under the hood?
When you trigger the Smart Action from the UI, your browser will make an API call:POST /forest/actions/mark-as-live.
If you want to customize the API call, check the list of available options.
The
data.attributes.ids key allows you to retrieve easily the selected records from the UI.The
data.attributes.values key contains all the values of your input fields (handling input values).Other properties of
data.attributes are used to manage the select all behavior.
Available Smart Action options
Here is the list of available options to customize your Smart Action:Rails
Want to go further with Smart Actions? Read the next page to discover how to make your Smart Actions even more powerful with Forms!
Available Smart Action properties
req.user
The JWT Data Token contains all the details of the requesting user. On any authenticated request to your Admin Backend, you can access them with the variablereq.user.
req.body
You can find important information in the body of the request.This is particularly useful to find the context in which an action was performed via a relationship.
Customizing response
Default success notification
Returning a 204 status code to the HTTP request of the Smart Action shows the default notification message in the browser. On our Live Demo example, if our Smart ActionMark as Live route is implemented like this:

Custom success notification
If we return a 200 status code with an object{ success: '...' } as the payload like this…

Custom error notification
Finally, returning a 400 status code allows you to return errors properly.
Custom HTML response
You can also return a HTML page as a response to give more feedback to the admin user who has triggered your Smart Action. To do this, you just need to return a 200 status code with an object{ html: '...' }.
On our Live Demo example, we’ve created a Charge credit card Smart Action on the Collection customersthat returns a custom HTML response.

/app/controllers/forest/customers_controller.rb

Setting up a webhook
After a smart action you can set up a HTTP (or HTTPS) callback - a webhook - to forward information to other applications.To set up a webhook all you have to do is to add a
webhookobject in the response of your action.
Webhooks are commonly used to perform smaller requests and tasks, like sending emails or impersonating a user.
Another interesting use of this is automating SSO authentication into your external apps.
Downloading a file
- Rails
- Django
- Laravel
On our Live Demo, the collection
Customer has a Smart Action Generate invoice. In this use case, we want to download the generated PDF invoice after clicking on the action. To indicate a Smart Action returns something to download, you have to enable the option download.Refreshing your related data
If you want to create an action accessible from the details or the summary view of a record involving related data, this section may interest you. In the example below, the “Add new transaction” action is accessible from the summary view. This action creates a new transaction and automatically refreshes the “Emitted transactions” related data section to see the new transaction.- Rails
- Django
- Laravel
Below is the sample code. We use the
gem 'faker' to easily generate fake data. Remember to add this gem to your Gemfile and install it (bundle install) if you wish to use it.Redirecting to a different page on success
To streamline your operation workflow, it could make sense to redirect to another page after a Smart action was successfully executed.It is possible using the
redirectTo property.The redirection works both for internal (
*.forestadmin.com pages) and external links.
External links will open in a new tab.
Enable/Disable a Smart Action according to the state of a record
Sometimes, your Smart Action only makes sense depending on the state of your records. On our Live Demo, it does not make any sense to enable theMark as Live Smart Action on the companies collection if the company is already live, right? This is configured from the collection’s Smart Action settings.
Restrict a smart action to specific roles
When using Forest collaboratively with clear roles defined it becomes relevant to restrict a smart action only to a select few. This functionality is accessible through Smart Actions Permissions in the Role section of your Project Settings.Require approval for a Smart action
Critical actions for your business may need approval before being processed. You can require approval per role from the Roles tab of your Project Settings; approval requests are then reviewed from the Collaboration menu.Want to go further with Smart Actions? Read the next page to discover how to make your Smart Actions even more powerful with Forms!