is it possible to use Turbo stream with radio buttons? It’s been working great so far with links and button, cause they fire an event, but I’d like to use it with radio buttons as well.
Whenever the radio button is checked, the template that uses Turbo stream should be rendered. Is that possible without writing an extra ajax request?
Hiding/showing turbo-frame makes Turbo irrelevant in this case. It’d be easier and there’d be less code if I just use these two radio buttons, use a little JS to hide/show a div and it does the same job. I would’ve hoped that there’s a solution where I can skip JS entirely, because using JS makes Turbo obsolete in this perticular case.
The are two radio buttons. If the user clicks on the first radio button, nothing’s supposed to happen because he admits that he didn’t get any donations. If the user checks the second radio button, the user admints that he received donations and needs to declare them.
These donations are input fields and are on another template.
The way to get this working is to treat it as though there was no JavaScript on the page at all. To do that, you would have these two radio buttons inside a single form, with a submit button. The form would submit to the controller, and depending on the choice made in this field (two or more radio buttons that share a name are treated as a single field, with multiple options), something would happen in the application.
So to then treat this with JavaScript, what you want to do is hook the buttons to submit the surrounding form. Long ago, before the war, you could do this with an onChange handler on each button, both set to the same method: this.form.submit(). Then you simply observe the form submission, handle it through an Ajax request, and take the return value as your decision point – redirect or not. Both options submit, and the controller on the server figures out what to do next.
I agree with @walterdavis suggestion to make the page work without javascript (turbo, or any other JS library), then enhance the experience with JS.
Otherwise, to answer your original question… without a submit button in this scenario, Turbo does not provide this functionality out-of-the-box. You would need to add some stimulus or other javascript.