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:
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:
- I have a controller called pages and it looks like this.
Then you generate a new stimulus controller caller ajax and put something like this.
If you have better answer, please add a comment about this, thank you!