16

In "Agile Web Development with Rails" (third edition) page 537 - 541 it has "Custom Form Builders" code as follows:

  class TaggedBuilder < ActionView::Helpers::FormBuilder
    # <p> # <label for="product_description">Description</label><br/> # <%= form.text_area 'description' %> #</p>
    def self.create_tagged_field(method_name) 
      define_method(method_name) do |label, *args|
        @template.content_tag("p" , @template.content_tag("label" , label.to_s.humanize, 
        :for => "#{@object_name}_#{label}") + "<br/>" + super)
      end
    end
    field_helpers.each do |name| 
      create_tagged_field(name)
    end 
  end

This code doesn't work with Ruby 1.9.1. It returns error as follows:

implicit argument passing of super from method defined by define_method() is not supported. Specify all arguments explicitly. (ActionView::TemplateError)

My question is: What should I change in the code to fix this?

MackM
  • 2,906
  • 5
  • 31
  • 45
jaycode
  • 2,926
  • 5
  • 35
  • 71

2 Answers2

21

The super above passed all parameters (see this recent question).

As the error message states, you must here "specify all arguments explicitly". Replace super with super(label, *args).

Community
  • 1
  • 1
Marc-André Lafortune
  • 78,216
  • 16
  • 166
  • 166
17

I encountered with this problem in a define_method without arguments

define_method :"#{info_type}_info" do

  info = super
  .......

end

And still found this problem. I had to explicit put the parenthesis:

define_method :"#{info_type}_info" do

  info = super()
  .......

end
Brenes
  • 311
  • 3
  • 6
  • 1
    that looks like a bug in the ruby parser to me. you might want to submit a report to the ruby core team. not sure if it's something they can fix or not. requiring parens doesn't look like ruby to me. :) – Stephen C Apr 26 '13 at 15:00
  • 3
    It doesn't appear to be a bug. See [this question](http://stackoverflow.com/questions/2570428/constructor-overriding), and [this form thread](https://www.ruby-forum.com/topic/146693). – Jared Beck Jul 08 '13 at 04:45