[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

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

Joshua Muheim

3/20/2007 10:47:00 PM

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

--
Posted via http://www.ruby-....