【问题标题】:Controller function to coffeescript咖啡脚本的控制器功能
【发布时间】:2018-11-06 14:29:59
【问题描述】:

我正在尝试在客户端的控制器中进行计算。这是函数

  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

这是正确的 CoffeeScript 吗?我不熟悉,遇到麻烦。这是我目前所拥有的,但我不确定它是否正确,我不确定如何调用它。

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)

应该从这个表单中调用并在模态中使用。

      <%= 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 %>

【问题讨论】:

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


    【解决方案1】:

    你的名字无处不在。 Savings_goal 应该是 SavingsGoal 等等。您在 coffescript 方法中缺少一些 if@ 表示 this 在您将变量作为参数传递给函数时您不想在这些方法中执行此操作。我不记得coffeescript 中的number? 函数了。

    我会为你修两个,剩下的留给你:

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

    【讨论】:

    • @ThomasRKoll 我根据您的反馈修复了 CoffeeScript。我在视图中遗漏了什么吗?
    • 使用你给我的我在price = document.getElementsByName('house_amount')[0].value; 上的控制台Uncaught TypeError: Cannot read property 'value' of undefined 中收到以下错误
    • 我编辑了你给我的行给price = document.getElementsByName('house_amount'),它不再抛出错误。
    • 我认为你不应该在得到答案后编辑你的问题,因为现在我的答案不再符合你的问题了。
    • 关于 javascript 中事物的命名,它与 Ruby 不同,我强烈建议阅读有关该主题的指南。当您与其他更有经验的开发人员合作时,它将为您提供帮助w3schools.com/js/js_conventions.asp
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-09
    • 1970-01-01
    • 2012-10-01
    • 2012-07-26
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多