Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a form helper method

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?

ideally id like to be able to call:

f.nice_editor :field
like image 973
Arcath Avatar asked Jan 24 '10 18:01

Arcath


People also ask

What is form helper in Rails?

Forms in web applications are an essential interface for user input. However, form markup can quickly become tedious to write and maintain because of form control naming and their numerous attributes. Rails deals away with these complexities by providing view helpers for generating form markup.

What is helper form in codeigniter?

The Form Helper file contains functions that assist in working with forms. Loading this Helper. Escaping field values. Available Functions.


1 Answers

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.

like image 92
Lex Lindsey Avatar answered Oct 14 '22 04:10

Lex Lindsey