Ive made a nice editor in jQuery and i want to add it as a form helper method.
how would i go about making a new form helper method开发者_如何学JAVA?
ideally id like to be able to call:
f.nice_editor :field
Part of the question is: where do you put the nice_editor code? I don't think it is a good idea to directly edit class ActionView::Helpers::FormBuilder in your installation. Instead, put your code in one of the files in app/helpers. There are several ways to add extension methods to FormBuilder.
For instance, suppose you have a helper file items_helper.rb:
module ItemsHelper
# this is one way to define new instance methods
ActionView::Helpers::FormBuilder.class_eval do
def nice_editor(conf,*opts)
...
end
end
end
Also, see this good discussion, which shows how to use self.included() to extend FormBuilder.
The object yielded to form_for
is an instance of ActionView::Helpers::FormBuilder
. So all you have to do is to add instance methods there.
class ActionView::Helpers::FormBuilder
def custom_field(...)
...
end
end
after some research i found that this works:
class ActionView::Helpers::FormBuilder
def nice_editor(conf)
#stuff to draw the editor
end
end
"conf" would have all the symbol options passed to it from the view. it works fine with f.
精彩评论