I am currently in the process of integrating Ajax into a non-database form calculator in Rails. However, I am encountering an issue where it does not seem to be responding and is giving me a 204 no content server answer. I have been trying to troubleshoot this problem on my own, but I find myself stuck. Since my knowledge of JavaScript is quite basic, a detailed response would be greatly appreciated. The calculator works perfectly fine without Ajax and passes all tests.
Calculator Controller
class InterestCalculatorController
def new
respond_to do |format|
format.html { render 'index.html.erb' }
format.js
end
# If accepted parameter is integer, then it shows in view as 5, when it
# is float, it shows as 5.1
@first_0 = params[:a_0].to_f % 1 != 0 ? params[:a_0].to_f : params[:a_0].to_i
@second_0 = params[:b_0].to_f % 1 != 0 ? params[:b_0].to_f : params[:b_0].to_i
# How many percent is number from the number
number_to_number(@first_0, @second_0)
private
def number_to_number(a = 0, b = 0)
# If the first number is zero, it sends 0% answer. If the second number is zero
# and the first number is nonzero, it sends infinity. Otherwise simple formula calculation.
if a.zero?
@result_0 = 0
elsif b.zero?
@result_0 = "infinity"
else
@result_0 = a.to_f / b.to_f * 100
end
end
end
index.js.erb
document.getElementById("answer_0").innerHTML = <%= @result_0 %>
View index.html.erb
<h1>Interest Calculator</h1>
<div id="interest_calculator_main">
<div id="interest_calculator">
<%= form_for :interest_calculator, url: { action: :new }, method: :get, remote: true do |f| %>
<p>How much % is one number from another?</p>
<%= number_field_tag :a_0, params[:a_0], step: :any, id: "first_number_0" %>
<p>of the number</p>
<%= number_field_tag :b_0, params[:b_0], step: :any, id: "second_number_0" %>
<%= f.submit 'Calculate!', id: "number_to_number" %>
<% end %>
<% unless @result_0.nil? %>
<p>Number <%= @first_0 %> from number <%= @second_0 %> = <label id="answer_0">%</label></p>
<% end %>
</div>
</div>