I have a form that makes a request to a non-CRUD service class. It is a loan calculator that requires the amount, interest rate, and length. This calculator is tied to a Quote
model, but since the service class is not creating or updating any records I am using a generic form.
I would like the form to submit and replace a frame on the same page so the returned data is represented on the page.
When I submit my form I get Error: Form responses must redirect to another location
. I do not want to redirect to another location - all I want to do is render the returned turbo frame.
My question is - how do I submit inputs and return the frame on the same page? Is this a case where I should be using Turbo Streams instead? It seems like a case for Frames since I am only updating 1 part of the page. Why do I have to redirect after a form submission?
The route I am using is POST
/quotes/loan_calculator
in case that impacts anything.
quotes_controller.rb
def loan_calculator
@lease_option = LoanCalculator.new(quote_params).run
render partial: "quotes/shared/loans/loan_options"
end
new.html.erb
<%= form_with url: loan_calculator_quotes_path do |form| %>
<div>
<%= form.label "quote[term_months]", "Months" %>
<%= form.number_field "quote[term_months]", required: true, min: 0 %>
</div>
<div>
<%= form.label "quote[interest_rate]", "Interest Rate" %>
<%= form.number_field "quote[interest_rate]", required: true, min: 0 %>
</div>
<div>
<%= form.label "quote[amount]", "Amount" %>
<%= form.number_field "quote[amount]", required: true, min: 0 %>
</div>
<%= form.submit "Calculate" %>
<% end %>
<%= turbo_frame_tag :lease_option do %>
<div>
<p>Base Payment</p>
</div>
<div>
<p>Total Lease Amount</p> <%= %>
</div>
<% end %>
_loan_options.html.erb
<%= turbo_frame_tag :loan_option do %>
<div>
<p>Base Payment</p> <%= @loan_option.payment %>
</div>
<div>
<p>Total Loan Amount</p> <%= @loan_option.total_payment %>
</div>
<% end %>