【问题标题】:Making a calculation for a modal using coffeescript使用咖啡脚本计算模态
【发布时间】:2018-11-07 13:11:26
【问题描述】:

我正在尝试使用户可以输入 3 个数字,当他们单击提交按钮时,他们将看到一个模态,其中包含模态中这 3 个数字的计算结果。我不确定如何做到这一点。我的控制器中有计算。

class WelcomeController < ApplicationController

  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"
    end
    respond_to do |format|
        format.json { render json: {:months_to_buy => @months_to_buy, :total_savings => @total_savings}}
      end
    end

end

我的表格如下...

      <%= form_tag( '/welcome/how_much', post: true, remote: true) do %>
       <h5 class="label">Estimated new home cost?</h5>
        <%= text_field_tag 'price', nil, placeholder: 'ex. 100,000', class: "form-control form-control-lg" %>

       <h5 class="label">Estimated payment for a new home?</h5>
        <%= text_field_tag 'mortgage', nil, placeholder: 'ex. 1,200', class: "form-control form-control-lg" %>

       <h5 class="label">Current Monthly Rent?</h5>
        <%= text_field_tag 'rent', nil, placeholder: 'ex. 800', class: "form-control form-control-lg" %>

       <button type="button" class="btn btn-success btn-lg" data-toggle="modal" data-target="#savingsModal">
      See how we help
    </button>

<!-- 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 towards a down payment & ...</h5>
      <div class="modal-body">
        <h4>Sign-up Now to get started!</h4>
      </div>
    </div>
  </div>
</div>
<% end %>

最后,这是我拥有的 CoffeeScript。它也有计算,但我不确定是否应该在 CoffeeScript 或控制器中进行计算。我不知道我这样做是对的还是我完全不在基地,请帮忙!!!!

# Calculate Savings jQuery
price = document.getElementsByName('house_amount')[0].value
mortgage = document.getElementsByName('high_rent')[0].value
rent = document.getElementsByName('current_rent')[0].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)

【问题讨论】:

  • 计算对您的业务至关重要吗?就像,这会定义用户是否可以获得帐户吗?因为如果是这样,计算应该单独完成 - 或至少验证 - 服务器端。如果信息不是很重要,但对用户来说很好,我不会费心在服务器端做。
  • 不重要,所以我认为客户端可以工作。我需要有关如何在客户端实施的帮助。
  • 另外:您应该考虑:如果信息不重要,您为什么要收集它?
  • 我不需要收集它。只需要它来运行计算,该计算将显示在同一页面上的模式中。

标签: html coffeescript ruby-on-rails-5


【解决方案1】:
class Calculation
  constructor (@price, @mortgage, @rent) -> 
  monthly_savings -> @mortgage - @rent
  savings_goal -> @price * 0.03
  months -> Math.ceil @savings_goal / @monthly_savings
  savings -> monthly_savings * months

并创建计算实例:

price = document.getElementsByName('house_amount')[0].value
mortgage = document.getElementsByName('high_rent')[0].value
rent = document.getElementsByName('current_rent')[0].value
instance = new Calculation(price, mortgage, rent)

然后在某处显示它:

document.getElementById('total_savings').value = instance.savings();

【讨论】:

  • 好的,所以完全理解你的答案。所有这些都在 js.coffee 文件中正确吗?如何在模态中调用答案?
  • 另外,我在这一行 price = document.getElementsByName('house_amount')[0].value 收到此错误 price = document.getElementsByName('house_amount')[0].value
  • 页面加载时输入为空。它应该只在单击提交按钮时查找输入吗?这会修复错误吗?
  • 好的,所以 coffeescript/javascript 抛出错误,因为在页面加载时输入是空的.我相信这会解决它,但我仍然不确定如何将其发送回模态。
  • 也许你应该从一些更简单的例子开始,了解 javascript、CoffeeScript 和你的 modals 是如何单独工作的。当您似乎难以掌握应用程序的每个单独部分时,很难为您提供指导。但话虽如此,您应该只在需要时运行instance.savings() 方法——也就是说,当用户按下某个按钮或采取行动表明他想要结果时。
猜你喜欢
  • 1970-01-01
  • 2021-08-17
  • 2013-01-09
  • 1970-01-01
  • 2012-10-12
  • 1970-01-01
  • 2011-11-08
  • 2011-06-30
  • 2013-06-10
相关资源
最近更新 更多