My project involves a basic Ruby app built on Sinatra. I am currently working on implementing a feature where a form can dynamically update the page based on user input. For instance, when a user enters their name in a form field and hits "go," the page should display a personalized greeting like "Welcome {name}" without requiring a page refresh.
Here is the relevant code that I have functioning:
In my app.rb file:
get '/examples' do
erb :example
end
post '/examples' do
post = params[:post]
@name = post['name']
puts "HERE"
erb :example
Snippet from my example.erb file (showing only the form):
<% if @name then %>
<%= @name %>
<% end %>
<form id="myForm" action="/examples" method="post">
Name: <input type="text" name="post[name]" />
<input type="submit" value="Submit Name" />
</form>
Additionally, here is the JavaScript file snippet:
// bind 'myForm' and provide a simple callback function
$('#myForm').ajaxForm(function() {
alert("Thank you for your comment!");
});
The issue arises after the execution of the post route in the app.rb file; despite seeing "HERE" printed in the terminal and receiving an alert, the page fails to update. I attempted applying a redirect call in the post route as well, but it did not resolve the problem. Any suggestions or solutions?