0

I am using a datatable in my rails3.1 app and it works, until I try and use a routing path for one of the returned fields (second last field). It doesn;t matter what valid routing path I use, I receive the error NoMethodError (undefined method 'active_toggle_user_path' for #<UsersDatatable:0x007f9d16cf0188>):

Any suggestions?

def data
  users.map do |user|
    [
      link_to(user.last_name, user),
      user.first_name,
      user.username,
      user.email,
      user.security_role,
      user.last_login_at,
      user.active == true ? 'Disable' : link_to('Enable', active_toggle_user_path(user, :toggle => true), { :confirm => 'Are you sure you want to ENABLE this User?', :class => "enable" }),
      'Delete User?'
    ]
  end
end
Raidri supports Monica
  • 15,886
  • 7
  • 49
  • 57
Coderama
  • 9,906
  • 12
  • 39
  • 55

1 Answers1

3

Solved using Rails.application.routes

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

  def data
    users.map do |user|
      [
        link_to(user.last_name, user),
        user.first_name,
        user.username,
        user.email,
        user.security_role,
        user.last_login_at,
        user.active == true ? 'Disable' : link_to('Enable', url_helpers.active_toggle_user_path(user, :toggle => true), { :confirm => 'Are you sure you want to ENABLE this User?', :class => "enable" }),
        'Delete User?'
      ]
    end
  end
end
Coderama
  • 9,906
  • 12
  • 39
  • 55
  • Was causing me a lot of issues - Didn't even know how to leverage delegate like this. Very useful even 5 years later. – DNorthrup Sep 11 '17 at 20:49