Hello everyone!
I am currently utilizing Rails and in my process, I would like the user to perform the following steps on the same page:
Input their address Completing the form
Submit the form Clicking to submit
Update the view to display the address View updated
Should I utilize Stimulis or Ajax? I'm unsure which would be more advantageous! I attempted using plain JS but it ended up being repetitive and not very straightforward:
// file.js
document.querySelector(".form").addEventListener('submit', function () {
address_form = document.querySelector(".address_form");
address_form.style.display="none";
document.location.reload();
display_address = document.querySelector(".display_address");
display_address.style.display = "block";
});
#file.html.erb
<div class="display_address">
<% if current_user.line1? && current_user.postal_code? && current_user.city? %>
<p>My current address: <%= current_user.line1 %>, <%= current_user.city %> <%= current_user.postal_code %></p>
<% end %>
</div>
<div class="address_form">
<%= simple_form_for(current_user, remote: true, html: { id: "add“dd”} ) do |f| %>
<%= f.input :line1, label: 'My address' %>
<%= f.input :city, label: 'City' %>
<%= f.input :postal_code, label: 'Postal Code' %>
<%= f.submit %>
<% end %>
</div>
In summary, I aim for the user to input their address in the form, submit it, update my database, and refresh the view with the newly submitted address.
Thank you for your assistance!