Action Component API
The action
component can be used to trigger asynchronous requests from - for example a button click - or any other html markup. The action
components let's us wrap content in an a
tag which is then clickable and triggers a background request with the configured request method to the configured path and with optionally given params and let's us react to the server response. It can distinguish between a successful and failed response and emit events, transition somewhere, completely redirect and more. You only need to configure it according to your needs.
Parameters
The core action component accepts the following parameters:
Method - required
This specifies which kind of HTTP method should get triggered. It accepts a symbol like so:
Path - required
This parameter accepts a typical Rails path:
Data
Here, we can pass data with our request, e.g. in the form of a hash:
Confirm
When specified, a browser-native confirm dialog is shown before the action is actually performed. The action only is performed after the user confirms. The action is not performed if the user declines to confirm dialog.
If no text
is given, the default text "Are you sure?" will be used.
Emit
This event gets emitted right after triggering the action. In contrast to the sucsess
or failure
events, it will be emitted regardless of the server response.
Delay
You can use this attribute if you want to delay the actual action submit request. It will not delay the event specified with the emit
attribute.
Success
The success part of the action component gets triggered once the action we wanted to perform returns a success code, usually the 200
HTTP status code.
To trigger further behavior, we can configure the success part of an action to emit a message like so:
Perform transition
We can also perform a transition that only gets triggered on success and also accepts further params:
When the server redirects to a url, for example after creating a new record, the transition needs to be configured to follow this redirect of the server response.
A controller action that respond with the url the page should transition to, could look like this:
Perform redirect
We can also perform a redirect (full page load) that only gets triggered on success and also accepts further params:
Please be aware, that emiting a event doen't have an effect when performing a redirect instead of a transition, as the whole page (including the surrounding app) gets reloaded!
When the server redirects to a url, for example after creating a new record, the redirect needs to be configured to follow this redirect of the server response.
A controller action that respond with the url the page should transition to, could look like this:
Same applies for the failure
configuration.
Failure
As counterpart to the success part of the action component, there is also the possibility to define the failure behavior. This is what gets triggered after the response to our action returns a failure code, usually in the range of 400
or 500
HTTP status codes.
To trigger further behavior, we can configure the failure part of an action to emit a message like so:
We can also perform a transition that only gets triggered on failure:
ID
This parameter accepts a string of ids that the action component should have:
which renders as an HTML id
attribute, like so:
Class
This parameter accepts a string of classes that the action component should have:
which renders as an HTML class
attribute, like so:
Examples
See some common use cases for the action core component below:
Async request with payload
First, make sure our routes accept requests the way we want to use them. Modify them in config/routes.rb
After that, you can specify an action on our example page. Notice how we wrap a button to have something visible to click and trigger the action!
In this case, the ActionTestController
receives :foo => 'bar'
in the params.
Async request with URL param
Instead of sending raw data, we can also explicitly pass params to a route. Like in the example above, we open up the route we intend to use in config/routes.rb
:
And on the example page, we specify our action component's behavior:
This example simply sends the param :id => '42'
to the route we have defined!
Success/Failure Behavior
Now, we examine different cases on how to handle success/failure scenarios.
Again, we look at our routes beforehand. This time, we define two different endpoints in config/routes.rb
:
Let's also take a look at the app/controllers/action_test_controller.rb
to see what the endpoints do:
Async request with success event emit used for rerendering
Below, we define an action component and an async component. The async component is documented here, for now it is just important that it waits for our action_config
success message and will get re-rendered.
Now, if we click the button and everything goes well (which should be the case in this very simple example), we can see the timestamp gets updated - nice!
Async request with success event emit used for notification
In this example, we will show a message that gets triggered once the controller returns a status code of 200
:
This time, after clicking our action component we should see the good job!
message that was initially hidden and disappears again after 300ms.
Async request with failure event emit used for notification
In the examples before, we always assumed (and made sure) that things went well. Now, it's the first time to use the failure_action_test_path
to see how we can notify the user if things go wrong!
Now, clicking the button shows the failure message - just as we expected it to!
Async request with success event emit used for transition
Unlike before, we will use the action component to trigger a page transition!
Again, we start by defining our routes in config/routes.rb
:
Our example app layout, already including placeholders for success/failure notifications:
To make a transition from one page to the other work, we need to make both of them available in our controller:
The first page, including an action component that performs a page transition to page 2 on success!
The second page, including an action that shows us the failure message we defined in the controller and then transfers us back to page 1.
Now, we can visit localhost:3000/action_test/page1
and see our first page, shown by the This is Page 1
text.
There, we can click on our button (Click me!
) and get transfered to the second page. There, we see the This is Page 2
text and, for 300ms, our server says: good job!
success message. Neat!
If we click the button (Click me!
) on the second page, we get the failure message (server says: something went wrong!
) and get sent back to page 2, just as we wanted to.
Last updated