undefined method `def_delegator' for Datatable:Class
def_delegators ruby
ruby undefined method delegate
i have build a datatable and try to use link_to and _path helper within my datatable.rb. I get an "undefined method" error, when using def_delegator. According to this link it should work. Why am i not able to use def_delegator ?
datatable.rb:
class NetworkDatatable < AjaxDatatablesRails::ActiveRecord def_delegator :@view, :link_to def_delegator :@view, :network_path def view_columns @view_columns ||= { network: { source: "Network.network"}, comment: { source: "Network.comment"} } end def data records.map do |record| { network: link_to(record.id, "#", network_path(record.id), :onclick => "display_dialog(); return(false);"), comment: record.comment } end end def get_raw_records Network.all end end
error:
Completed 500 Internal Server Error in 9ms (ActiveRecord: 0.0ms) NoMethodError (undefined method `def_delegator' for NetworkDatatable:Class): app/datatables/network_datatable.rb:4:in `<class:NetworkDatatable>' app/datatables/network_datatable.rb:1:in `<top (required)>'
def_delegator
is a method on Forwardable class
In order to use it you will have to do the following:
require 'forwardable' class NetworkDatatable < AjaxDatatablesRails::ActiveRecord extend Forwardable def_delegator :@view, :link_to end
NoMethodError (undefined method `def_delegators' for , I have this error: ```NoMethodError (undefined method `def_delegators' for ChatroomsDatatable:Class):``` In v1.2.0, def_delegator :@obj, :data, :fallback_data would return :fallback_data but v1.3.0 just returns nil The cause for this is the last expression mod.send(:ruby2_keywords, ali) if RUBY_VERSION >= '2.7' in the following definition:
Maybe try the following instead
class NetworkDatatable < AjaxDatatablesRails::ActiveRecord delegate :link_to, :network_path, to: :@view end
def_delegator
is a Forwardable
method and I do not see the extension of this module. Although you could obviously extend your class with this module as others have proposed.
However Module#delegate
is a method rails has added to the Module
class (meaning it is accessible in almost every class) and should work consistently with the proposed.
Please note the method signatures differ slightly.
Forwardable#def_delegator
(ruby standard lib)
# def_delegator :target, :method
Forwardable#def_delegators
plural (ruby standard lib)
# def_delegators :target, :method1, :method2, ..., :method_n
Module#delegate
(rails only)
# delegate :method1, :method2, ..., :method_n, to: :target
Module: Forwardable (Ruby 2.4.0), Delegation refers to evaluating a member (property or method) of one object (the module and its def_delegator and def_delegators methods. "Doe")) decorated_user.first_name #=> NoMethodError: undefined method� NoMethodError: undefined method `model_name' for []:Array As you can see, def_delegator will not only forward the method call onto the delegate object, it will
The def_forwardable
method is defined in Ruby's standard library Forwardable
module, so the class needs to extend the module to be able to use it. There is an example of this method in the section Using View Helpers of the ajax-datatables-rails project.
require 'forwardable' class NetworkDatatable < AjaxDatatablesRails::ActiveRecord extend Forwardable def_delegator :@view, :link_to def_delegator :@view, :network_path end
Ruby's Hidden Gems: Delegator and Forwardable, NoMethodError: undefined method `def_delegators' for Stupidedi::Config:: FunctionalGroupConfig:Class #117. Closed. lkfken opened this issue� It defines a new method that delegates to the method of the same name in the given object. There's also a def_delegators method, which takes multiple method symbols as arguments and defines a delegator method for each one. By calling def_delegator multiple times, you can have a single Forwardable delegate different methods to different subobjects.
NoMethodError: undefined method `def_delegators' for Stupidedi , Upgraded from '0.4.0' to the latest version. Now I get: undefined method link_to' for nil:NilClass` Basically none of my def_delegators definitions� As you can see, def_delegator will not only forward the method call onto the delegate object, it will also forward any arguments provided as well. It is here that the bug arises: it splats all of the arguments into a variable which is called args, and because of how variable scope works in Ruby, it then attempts to call model_name on this 06/04/2020 3/10
def_delegator not working? � Issue #346 � jbox-web/ajax-datatables , You can delegate methods in Ruby using multiple techniques! undefined method `write' for #<Computer:0x000055c2e8f7d310> (NoMethodError) require 'forwardable'; class Computer; extend Forwardable; def_delegators :@ memory,� NoMethodError: undefined method loveliness for nil:NilClass I find this will come up when I think I'm operating on an object with methods, but I'm actually operating on a hash with attributes. my_hash [ :loveliness ] # "very lovely" yay, this is a thing that exists! my_hash . loveliness # NoMethodError: undefined method loveliness nil:NilClass
How To Delegate Methods in Ruby & Ruby on Rails, There's also a def_delegators method, which takes multiple method symbols as end a a a # = AppendOnlyArray << 4 << 5 a.size => undefined method `size'� The Invite model has been updated with the before_create filter and generate_token method. The Invites controller has been updated with the create action. However, when I visit the Calendar edit view to fill out the Invite form, I get the following error:
Comments
- You have typo in extend there ;]