检索重复费用时未定义的方法错误

时间:2014-08-01 22:06:15

标签: ruby-on-rails

我正在开发一个基于Rails 4.1的小型费用管理应用程序,我正在使用Ice Cube和Recurring Select宝石来创建经常性费用。

表格如下:

<%= form_for(@expense) do |f| %>
<div class="field">
    <%= f.label :description %><br>
    <%= f.text_field :description %>
  </div>
  <div class="field">
    <%= f.label :amount %><br>
    <%= f.number_field :amount %>
  </div>
  <div>
    <%= f.label :recurring_rules, "no defaults and not set" %><br/>
    <%= f.select_recurring :repeat %>
  </div>
  <div class="actions">
    <%= f.submit %>
  </div>
<% end %>

这是一个简单的脚手架,以这种格式保存重复规则:{"interval":1,"until":null,"count":null,"validations":null,"rule_type":"IceCube::DailyRule"}

我正试图暂时列出索引页面本身的所有经常性费用。索引操作如下所示:

 def index
    @expenses = Expense.all
    @exp = @expenses.all_occurrences
  end

相应的视图包含以下代码:

<h2>Recurring Expenses</h2>

<% @exp.each do |e| %>
  <%= e.description %>
  <%= e.amount %>
<% end %>

不幸的是,我得到了未定义的方法'all_occurrences'错误。我也尝试在控制器中使用@exp = @expenses.occurs_on?(Date.today),但我仍然得到未定义的方法错误。我在这里错过了什么吗?我是否必须在模型中声明关于Ice Cube的内容?

1 个答案:

答案 0 :(得分:0)

这是因为@expenses = Expense.all ActiveRecord_Relation 对象返回给@expenses。它是一个ActiveRecord类,因此您没有此类中称为occurs_on?的方法或任何其他自定义方法。

如果你想获得所有的出现次数,并且出事次数是费用的关系,而费用有很多次出现,那么这将解决:

@occurrences = Expense.occurrences