0

I have a reason to pass content_tag method from a helper into a module:

module MyHelper
  def helper_method1(a, b)
    MyModule.module_method1(a, b, &content_tag)
  end
end

def MyModule
  def self.module_method1(a, b, &content_tag)
    #......
    my_tag = content_tag.call(:span, nil, class: "some_class123")
  end
end

The error I received is wrong number of arguments (0 for 1+). What am I doing wrong?

Incerteza
  • 32,326
  • 47
  • 154
  • 261
  • I do not quite have enough context to understand this question. Could you provide your file structure, the requires and the actual call that fails? Is the line that fails actually the one with the `content_tag.call`? Maybe you should try to execute the `content_tag` method in the context of `MyHelper`? – Patru Apr 20 '14 at 17:29
  • @Patru, the requirement is pass content_tag to MyModule and call it. That's it. – Incerteza Apr 20 '14 at 17:32
  • Please share the complete error log. Also, how are you calling `helper_method1 `. Share the relevant code. – Kirti Thorat Apr 20 '14 at 20:04
  • Do you want to make this hard deliberately? I guess you want to write `module MyModule` instead of `def MyModule`, otherwise you will *only* get a `NameError: uninitialized constant MyModule`. – Patru Apr 20 '14 at 23:09
  • @Patru, the requirement is pass content_tag to MyModule and call it. That's it – Incerteza Apr 21 '14 at 02:12

1 Answers1

0

You make it in a wrong way, in your code you try co pass block (that doesn't exists) in method in this case your either have to pass this to your helper method, or pass view context inside and delegate content_at to this context.

Block delegation

 module MyHelper
  def helper_method1(a, b, &block)
    MyModule.module_method1(a, b, &block)
  end
 end

 def MyModule
   include ActionView::Helpers::TagHelper

   def self.module_method1(a, b, &block)
     #......
     my_tag = content_tag(:span, &block, class: "some_class123")
   end
 end

Pass view context

 module MyHelper
  def helper_method1(a, b)
    MyModule.module_method1(a, b, self)
  end
 end

 def MyModule
   def self.module_method1(a, b, view_context)
     my_tag = view_context.content_tag(:span, nil, class: "some_class123")
   end
 end

Take into account include ActionView::Helpers::TagHelper is crucial in first example

You could check it out Using link_to in a class in a Rails helper it could be useful as well

Community
  • 1
  • 1
shemerey
  • 501
  • 2
  • 5
  • sorry, I didn't get it. Why can't I pass `&content_tag` as a method to the module? `content_tag` does exist in the helper. – Incerteza Apr 23 '14 at 14:39