What's the difference here? (class extension on run-time)

J

Joshua Muheim

Hi all

I'm not very experienced with Ruby yet. I have the following code (from
a Ruby on Rails project) that I have adapted from a book and modified a
bit:

class IncenseFormBuilder < ActionView::Helpers::FormBuilder
private
def self.create_tagged_field(method_name)
define_method(method_name) do |label, *args|
html_surround(label, super) # Provides the HTML code that
surrounds the form label and the method (super) that generates the input
tag(s)
end
end

field_helpers.reject{|h| h == 'hidden_field'}.concat([:select]).each
do |method_name|
create_tagged_field(method_name)
end
end
end

This code works as expected: it defines some methods on run-time and I
have those neat methods available in my views.

I wanted to edit the code in the following way to make it shorter:

class IncenseFormBuilder < ActionView::Helpers::FormBuilder
private
field_helpers.reject{|h| h == 'hidden_field'}.concat([:select]).each
do |method_name|
define_method(method_name) do |label, *args|
html_surround(label, super) # Provides the HTML code that
surrounds the form label and the method (super) that generates the input
tag(s)
end
end
end
end

But now I get a strange "private method `text_field' called for
#<IncenseFormBuilder:0x22fd6b0>" error although the code seems to do
quite the same as before? What have i missed?

Thanks for help,
Joshua
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top