我正在使用Railscast的方法来远程访问dataTables的表。
这是workorders4_datatable.rb中代码的开头:
class Workorders4Datatable
delegate :params, :h, :link_to, :number_to_currency, to: :@view
def initialize(view)
@view = view
end
def as_json(options = {})
{
sEcho: params[:sEcho].to_i,
iTotalRecords: Workorder.external.count,
iTotalDisplayRecords: workorders.external.total_entries,
aaData: data
}
end
private
def data
workorders.external.map do |workorder|
[
link_to(workorder.id, workorder),
h(workorder.parent.try(:id)),
h(workorder.description),
h(workorder.client.try(:client_name)),
h(workorder.woasset.try(:assetnum)),
h(workorder.type.try(:typecode)),
h(workorder.billmethod.try(:method_name)),
h(workorder.workgroup.try(:group_name)),
h(workorder.employee.try(:employee_full_name)),
h(workorder.wostatus.try(:statuscode)),
h(workorder.expenses.tobill.sum("quantity") * workorder.expenses.sum("unitcost")),
h(workorder.events.tobill.sum("hours")),
h(workorder.events.sum("hours")),
link_to('ADD Invoice', new_invoice_path(:workorder_id => workorder), :class => 'btn btn-primary')
]
end
end
除了这一行之外,所有这些都有效:
link_to('ADD Invoice', new_invoice_path(:workorder_id => workorder), :class => 'btn btn-primary')
导致此错误:
NoMethodError - undefined method `new_invoice_path' for #<Workorders4Datatable:0x007f94513588a0>:
然而,以下内容的工作方式不同:
<%= link_to 'ADD Invoice', new_invoice_path(:workorder_id => @workorder.id), :class => 'btn btn-primary' %>
我有什么方法可以让它发挥作用吗?
答案 0 :(得分:6)
您是否尝试将new_invoice_path
替换为@view.new_invoice_path
?那可能有用。原因是因为new_invoice_path
应该在view
的上下文中调用,但它是在Workorders4Datatable
的上下文中调用的。
我在过去看过这种模式是一种捷径:
def v
@view
end
v.new_invoice_path