Action
The action component allows us to trigger async HTTP requests without Javascript!
Parameters
The core action component accepts the following parameters:
Method
This specifies which kind of HTTP method should get triggered. It accepts a symbol like so:
Path
This parameter accepts a classic Rails path, usually in the form of a symbol like so:
Params
Using the standard Rails params, we can pass information to our route!
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 would create a record and then 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 would create a record and then respond with the url the page should redirect 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 (optional)
This parameter accepts a string of ids that the action component should have:
which renders as an HTML id
attribute, like so:
Class (optional)
This parameter accepts a string of classes that the action component should have:
which renders as an HTML class
attribute, like so:
Notify
Not in use right now.
Examples
See some common use cases for the action core component below:
Example 1 - 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.
Example 2: 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!
Example 3: 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:
Example 3.1: 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!
Example 3.2: 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.
Example 3.3: 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!
Example 3.4: 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