Ruby on rails 计算每月总数
你好,我正在创建一个小应用程序来帮助我管理我的信用卡 我有一个用户模型和一个信用卡模型Ruby on rails 计算每月总数,ruby-on-rails,ruby-on-rails-5.1,Ruby On Rails,Ruby On Rails 5.1,你好,我正在创建一个小应用程序来帮助我管理我的信用卡 我有一个用户模型和一个信用卡模型 class User < ApplicationRecord # Include default devise modules. Others available are: # :confirmable, :lockable, :timeoutable and :omniauthable devise :database_authenticatable, :registerable,
class User < ApplicationRecord
# Include default devise modules. Others available are:
# :confirmable, :lockable, :timeoutable and :omniauthable
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable
has_many :credit_cards
end
class CreditCard < ApplicationRecord
belongs_to :user
def credit_available
limit - balance
end
def annual_interest
if self.balance && self.interestRate
self.balance * self.interestRate
else
"0.0".to_d
end
end
def minimum_payment
n = balance / 100
b = n * 1
i = balance * interestRate
c = i / 12
c + b
end
end
景色是这样的
<% if user_signed_in? %>
<h1>Credit Cards</h1>
<% if current_user.credit_cards.any? %>
<table class="mdl-data-table mdl-js-data-table">
<thead>
<tr>
<th class="mdl-data-table__cell--non-numeric"><%= link_to 'Name', :sort => 'card_NickName' %> </th>
<th class="mdl-data-table__cell--non-numeric"><%= link_to 'Provider', :sort => 'card_provider' %></th>
<th class="mdl-data-table__cell--non-numeric"><%= link_to 'Points', :sort => 'points_provider' %></th>
<th>%</th>
<th>Balance</th>
<th>Limit</th>
<th>Available</th>
<th>Min Payments</th>
<th colspan="3"></th>
</tr>
</thead>
<tbody>
<% @credit_cards.each do |credit_card| %>
<tr>
<td class="mdl-data-table__cell--non-numeric"><%= link_to credit_card.nickName, credit_card %></td>
<td class="mdl-data-table__cell--non-numeric"><%= credit_card.provider %></td>
<td class="mdl-data-table__cell--non-numeric"><%= credit_card.pointsProvidor %></td>
<td><%= number_to_percentage(credit_card.interestRate * 100, precision: 2) %></td>
<td><%= number_to_currency(credit_card.balance, unit: "$") %></td>
<td><%= number_to_currency(credit_card.limit, unit: "$") %></td>
<td><%= number_to_currency(credit_card.credit_available, unit: "$") %></td>
<td><%= number_to_currency(credit_card.minimum_payment, unit: "$") %></td>
<td></td>
<td></td>
<td></td>
</tr>
<% end %>
</tbody>
<tfoot>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td><%= number_to_currency(@credit_card_debt, unit: "$") %></td>
<td><%= number_to_currency(@credit_limit, unit: "$") %></td>
<td><%= number_to_currency(@available_credit, unit: "$") %></td>
<td><%= @total_monthly_payment %></td>
<td></td>
<td></td>
<td></td>
</tr>
</tfoot>
</table>
<br>
<%= link_to new_credit_card_path, class: 'mdl-button mdl-js-button mdl-button--fab mdl-button--colored' do %>
<i class="material-icons">add</i>
<% end %>
<% else %>
You have no Credit Cards, better
<%= link_to new_credit_card_path, class: 'mdl-button mdl-js-button mdl-button--fab mdl-button--colored' do %>
<i class="material-icons">add</i>
<% end %>
one.
<% end %>
<% else %>
<div class="mdl-cell mdl-cell--2-col mdl-cell--2-col-tablet"></div>
<div class="mdl-cell mdl-cell--8-col mdl-cell--8-col-tablet"><h1>Credit Card Smarts at your fingertips</h1></div>
<div class="mdl-cell mdl-cell--2-col mdl-cell--2-col-tablet"></div>
<% end %>
信用卡
'卡片\u昵称'%>
'卡\u提供程序'%>
'点\提供程序'%>
%
平衡
极限
可用
最低付款额
添加
你最好没有信用卡
添加
一个。
触手可及的信用卡智能
在我尝试计算@total_monthly_付款时出现以下错误之前,一切都很好
NameError in CreditCardsController#index
undefined local variable or method `balance' for #<CreditCardsController:0x00007fc18f7dfa48>
Extracted source (around line #5):
3
4
5
6
7
8
def minimum_payment
n = balance / 100
b = n * 1
i = balance * interestRate
c = i / 12
CreditCardsController索引中的name错误
未定义的局部变量或方法“balance”#
提取的源(第5行附近):
3.
4.
5.
6.
7.
8.
def最低付款额
n=余额/100
b=n*1
i=余额*利率
c=i/12
控制器
class CreditCardsController < ApplicationController
before_action :authenticate_user!
before_action :set_credit_card, only: [:show, :edit, :update, :destroy]
# GET /credit_cards
# GET /credit_cards.json
def index
@credit_cards = current_user.credit_cards
@credit_card_debt = current_user.credit_cards.sum(:balance)
@credit_limit = current_user.credit_cards.sum(:limit)
@available_credit = current_user.credit_cards.sum(:limit) - current_user.credit_cards.sum(:balance)
@total_monthly_payment = total_monthly_payment
end
# GET /credit_cards/1
# GET /credit_cards/1.json
def show
end
# GET /credit_cards/new
def new
@credit_card = current_user.credit_cards.build
end
# GET /credit_cards/1/edit
def edit
end
# POST /credit_cards
# POST /credit_cards.json
def create
@credit_card = current_user.credit_cards.new(credit_card_params)
respond_to do |format|
if @credit_card.save
format.html { redirect_to @credit_card, notice: 'Credit card was successfully created.' }
format.json { render :show, status: :created, location: @credit_card }
else
format.html { render :new }
format.json { render json: @credit_card.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /credit_cards/1
# PATCH/PUT /credit_cards/1.json
def update
respond_to do |format|
if @credit_card.update(credit_card_params)
format.html { redirect_to @credit_card, notice: 'Credit card was successfully updated.' }
format.json { render :show, status: :ok, location: @credit_card }
else
format.html { render :edit }
format.json { render json: @credit_card.errors, status: :unprocessable_entity }
end
end
end
# DELETE /credit_cards/1
# DELETE /credit_cards/1.json
def destroy
@credit_card.destroy
respond_to do |format|
format.html { redirect_to credit_cards_url, notice: 'Credit card was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_credit_card
@credit_card = CreditCard.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def credit_card_params
params.require(:credit_card).permit(:nickName, :provider, :pointsProvidor, :interestRate, :balance, :limit, :user_id )
end
end
class CreditCardsController
请帮忙 以以下方式修改模型方法:
def total_monthly_payment
n = current_user.credit_cards.sum(&:minimum_payment)
end
只是一个旁注:-
n = current_user.credit_cards.sum(&:minimum_payment)
首先,它将获得所有信用卡最低付款的数组,然后
这将一个接一个地迭代这个数组,并在其中循环。因此,对于100或1000条记录,这不是一种有效的方法
为此,应使用sql查询:-
n = current_user.credit_cards.sum(:minimum_payment)
这将只触发sql查询,有些类似于:-
从
信用卡
中选择总和(信用卡
最低付款
),请添加控制器的索引操作。@razvans已添加,但在我可以之前已由其他人完成。。。。我不认为这有多大的不同,但谢谢现在有人应该能够得到一个完整的答案。你能解释一下-:&意味着你正在通过一个街区以备将来参考吗
n = current_user.credit_cards.sum(:minimum_payment)