coffeescript 的控制器功能
Controller function to coffeescript
我正在尝试在客户端的控制器中进行计算。这是函数
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 %>
你的名字到处都是。 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
我正在尝试在客户端的控制器中进行计算。这是函数
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 %>
你的名字到处都是。 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