我正在通过StackSkills处理一个类,他们的问题链接不见了。
我正在制定一个商业计划。到目前为止,我已经建立了数据库来存储发票信息(日期、税率、销售人员、公司名称)。我按照说明在发票列表中创建了一个搜索字段,允许我按日期范围进行搜索。当我尝试加载发票列表时,我收到一个错误,上面写着:
ArgumentError in InvoicesController#index
wrong number of arguments (given 1, expected 0)
Extracted source (around line #7):
5 # GET /invoices.json
6 def index
7 @search = InvoiceSearch.new(params[:search])
8 @invoices = @search.scope
9 end
这是为本练习添加到InvoicesController.rb文件中的代码:
def index
@search = InvoiceSearch.new(params[:search])
@invoices = @search.scope
end
这是本课中创建的invoice_Search.rb:
class InvoiceSearch
attr_reader :date_from, :date_to
def initializ(params)
params ||={}
@date_from = parsed_date(params[:date_from], 7.days.ago.to_date.to_s)
@date_to = parsed_date(params[:date_to], Date.today.to_s)
end
def scope
Invoice.where('date BETWEEN ? AND ?', @date_from, @date_to)
end
private
def parsed_date(date_string, default)
Date.parse(date_string)
rescue ArgumentError, TypeError
default
end
end
这就是本课添加到我的index.html.erb文件中的内容(在数据库中列出发票的页面):
<div class="row">
<h1>Listing Invoices</h1>
<div class="pull-right range-query">
<%= form_tag invoices_path, method: :get do %>
<%= text_field_tag 'search[date_from]', @search.date_from %>
<%= text_field_tag 'search[date_to]', @search.date_to %>
<%= submit_tag 'Search', class: 'btn search-button' %>
<% end %>
</div>
</div>
更改
initializ
至:
initialize