2013-10-04 69 views
1

我正在使用Railscast的方法來遠程訪問數據表的表格。Rails dataTables add link_to

這是代碼的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

忽略最後的評論。這是一個幫手代表團問題。請參見頂部代表:@ view'。這就是爲什麼'link_to'工作正常,但路徑不正確。你也可以委託':new_invoice_path',或者'@ view.new_invoice_path'。 –

+0

感謝您的幫助。我不確定你在暗示我改變了什麼。我是否將'委託添加到:@ view.new_invoice_path'? – Reddirt

+0

工作!如果你回答,我會很樂意接受它。 – Reddirt

回答

5

您是否試圖用@view.new_invoice_path替換new_invoice_path?這可能會起作用。原因是因爲new_invoice_path應在view的環境中調用,但在Workorders4Datatable的環境中調用。

我已經看到了這個模式在過去作爲快捷方式:

def v 
    @view 
end 

v.new_invoice_path