0

I have a file called messages_datatables.rb inside /app/datatables/admin/

class Admin::MessagesDatatable
  delegate :params, :h, :link_to, :number_to_currency, to: :@view
  .
  .
  .
  private
  def data
    messages.map do |message|
      [
        "", 
        link_to(message.subject, admin_message_path(message))
      ]
    end
  end
  .
  .
  .
  .
end

I need use link_to helper inside this file but I get:

NoMethodError (undefined method `admin_message_path' for #<Admin::MessagesDatatable:0xbe07170>):

The path is working fine in views. I have the path in my routes.

Where have I the error?

hyperrjas
  • 10,666
  • 25
  • 99
  • 198
  • 1
    Do you have the admin_message path? You can check by executing `rake routes` in your command line. There should be a line containing `admin_message`. You can also check your routes.rb if there is a route with something like `:as => 'admin_message'` – buftlica Nov 29 '12 at 18:20
  • Restart your web app server and try again after you change your routes – Sully Nov 29 '12 at 18:22
  • I wonder if the helper code for routes needs to be included before the class. – Tom Harrison Nov 29 '12 at 18:25
  • Thank you but after restart the server still not working... – hyperrjas Nov 29 '12 at 18:26
  • I'm guessing the *_path and *_url methods are defined somewhere else in a module or class which your class does not include or extend. – buftlica Nov 29 '12 at 18:29

1 Answers1

1

Add:

delegate :url_helpers, to: 'Rails.application.routes'

And instead of admin_message_path, use url_helpers.admin_message_path

apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • Thank you, this working fine if the app have not `params[:locale]`. My app use params locale inside routes something like `scope '(:locale)' do ...resources... end` I have tried with `link_to(message.subject url_helpers.admin_message_path(params[:locale], message))` but I get `NoMethodError (undefined method 'link_to' for #):` – hyperrjas Nov 29 '12 at 18:57
  • You shouldn't create an object aware of the full stack. – apneadiving Nov 29 '12 at 19:00
  • I am following this tutorial http://railscasts.com/episodes/340-datatables, where I can see how it works but `link_to` is not working for me! – hyperrjas Nov 29 '12 at 19:03
  • I can't see why link_to isn't recognized in your case. But you should pass params another way – apneadiving Nov 29 '12 at 23:21
  • The problem was fixed. Now is working fine with `link_to(message.subject url_helpers.admin_message_path(params[:locale], message))`. Thank you very much! – hyperrjas Nov 30 '12 at 08:15