Search code examples
htmlcoffeescriptruby-on-rails-5bootstrap-modal

Controller function to coffeescript


I am trying to do a calculation that was done in the controller on the client side. Here is the function

  def how_much
    @price = (params[:amount])
    @mortgage = (params[:high_rent])
    @rent = (params[:current_rent])

    if @price && @mortgage && @rent.present?
      @monthly_savings = @mortgage - @rent
      @savings_goal = @price*0.03
      @months_to_buy = (@savings_goal/@monthly_savings).to_i
      @total_savings = @monthly_savings * @months_to_buy
    else
      @months_to_buy = 24
      @total_savings = "great savings"

    respond_to do |format|
        format.json { render json: {:months_to_buy => @months_to_buy, :total_savings => @total_savings}}
      end
    end

Is this correct CoffeeScript? I am not familiar and having trouble. This is what I have so far but I unsure it is correct and I am unsure how to call it.

price = document.getElementsByName('house_amount').value
mortgage = document.getElementsByName('high_rent').value
rent = document.getElementsByName('current_rent').value
MonthlySavings: (mortgage, rent) ->
 if mortgage? && rent?
   parseFloat(mortgage) - parseFloat(rent)
SavingsGoal: (price) ->
 if price?
   parseFloat(price) * 0.03
MonthsToBuy: (Savings_goal,MonthlySavings) ->
 if SavingsGoal? && MonthlySavings?
   parseFloat(SavingsGoal)/parseFloat(MonthlySavings)
TotalSavings: (MonthlySavings,MonthsToBuy) ->
 if MonthlySavings? && MonthsToBuy?
   parseFloat(MonthlySavings) * parseFloat(MonthsToBuy)

It should be called from this form and used in the modal.

      <%= form_tag( '/welcome/how_much', post: true, remote: true) do %>
      <span id="questions">
        <h5 class="label">Estimated home cost?</h5>
        <%= text_field(:amount, {id: "house_amount", placeholder: "ex. 100,000"}, class: "form-control form-control-lg") %>
      </span>
      <span id="questions">
        <h5 class="label">Estimated payment</h5>
        <%= text_field(:high_rent, {id: "high_rent", placeholder: "ex. 1,200"}, class: "form-control form-control-lg") %>
      </span>
      <span id="questions">
        <h5 class="label">Current Monthly Rent?</h5>
        <%= text_field(:current_rent, {id: "current_rent", placeholder: "ex. 800"}, class: "form-control form-control-lg") %>
      </span>
  </div>
     <!----Should call modal and run coffescript calculation--->
        <%= submit_tag("See how quickly you can buy a home", data: {'data-toggle' => "modal", 'data_target' => "#savings_modal"}, class: "btn btn-success btn-lg") %>

</div>

<!-- Modal for sign-up -->
<div class="modal" id="savings_modal" tabindex="-1" role="dialog">
  <div class="modal-dialog" role="document">
    <div class="modal-content">
      <h3 class="modal-title" id="savingsModalTitle">You could be ready to buy in <%= @months_to_buy %> months</h3>
      <h5 class="modal-title">and have <%= @total_savings %>* to put down</h5>
      <div class="modal-body">
        <h4>Sign-up Now to get started!</h4>
        <%= render '_tenant_signup_modal_form.html.erb' %>
      </div>
    </div>
  </div>
</div>
<% end %>

Solution

  • Your naming is all over the place. Savings_goal should be SavingsGoal and so on. You're missing a few if in your coffescript methods and @ means this which you don't want to do in those methods as you pass the variables as argument to the functions. I can't remember a number? function in coffeescript.

    I'll fix two for you an leave the rest for you:

     MonthlySavings: (mortgage, rent) ->
       if mortgage? && rent?
         parseFloat(mortgage) - parseFloat(rent)
     SavingsGoal: (price) ->
       if price?
         parseFloat(price) * 0.03