Resolved: How to add a user input to re-calculate code in controller and then show in the same view?

In this post, we will see how to resolve How to add a user input to re-calculate code in controller and then show in the same view?

Question:

Looking to be pointed in the right direction. I have a simple form in my view that accepts a number. I am wanting to send the input of this to be used in a calculation in my controller and then update for the user to see in real time.
index.html.erb Simple input form, followed by calculation output:
Calculation in controller:
The code is just a stupid example, but the idea is: User input in the view, which I could then place in a calculation in my controller and then send back to view. I don’t need a database as I don’t need to store any information.
After researching, a lot of people are suggesting Ajax for this in rails? Is this the way to go or is there something in Ruby that I can use for the same effect. I’ve also seen Tableless Models being used. What would the best solution be here for me? I’m guessing recalculating the code in controller is the key bit.

Best Answer:

Yes, you can do this with AJAX. In Rails, you can use a Stimulus controller to do that.
Below is a simple demonstration:
  1. I have a controller called pages and it looks like this.

Then pages#index view should be like this.
It has your form and p tag with data-ajax-target=”output” is the element where you display form input data.
Then you generate a new stimulus controller caller ajax and put something like this.
And you need to make sure that you define a post route.
Hope that make sense.

If you have better answer, please add a comment about this, thank you!

Source: Stackoverflow.com