|
|
|
|
|
by edwinvlieg
1917 days ago
|
|
The whole idea of Hotwire is to prevent `Rails.ajax` calls. You can easily accomplish this with less code: 1. Replace the `output` target with a Turbo frame.
2. Add a value to the `data-controller` div with a `preview-url`: https://stimulus.hotwire.dev/reference/values
3. Change the `Rails.ajax` call with `let url = URL.new(this.previewUrlValue); url.searchParams.append('body', this.tweetTarget.value; this.outputTarget.src = url.toString();`
4. Change the `preview` action to render HTML with the same frame. Now you have an automatic refreshing frame with less code. |
|
You can also have a hidden form submission that does this from the existing controller so you don't need to specify the URL.
For example, you can add a hidden submit button like
Instead of a stimulus target, you wrap your preview area in a turbo frame In your (ruby) controller you can re-use the existing controller action: Do the turbo junk in a private method for the preview: Your stimulus controller now just does this: I'm not sure which I prefer!