ruby - Adding rails 3 form helpers to rails 2 -


i add number_field form helper exists in rails 3 rails 2.3.15 app, i'm having trouble extending module.

these methods need rails 3

class instancetag     def to_number_field_tag(field_type, options = {})         options = options.stringify_keys         if range = options.delete("in") || options.delete("within")           options.update("min" => range.min, "max" => range.max)         end         to_input_field_tag(field_type, options)       end end  def number_field(object_name, method, options = {})         instancetag.new(object_name, method, self, options.delete(:object)).to_number_field_tag("number", options) end  def number_field_tag(name, value = nil, options = {})         options = options.stringify_keys         options["type"] ||= "number"         if range = options.delete("in") || options.delete("within")           options.update("min" => range.min, "max" => range.max)         end         text_field_tag(name, value, options) end 

i'm adding module include in application helper. to_number_field_tag method easy because can open class , add override.

the formhelper module methods i'm having trouble because can't quite figure out ancestors chain , don't know how scope override. don't know how make work basically.

my problem above was not overriding formbuilder. here solution might need in future.

rather implementing type="number" input type, decided make generic helper new html5 inputs. place code in overrides file include in application_helper.rb.

# file 'rails_overrides.rb`  actionview::helpers::instancetag.class_eval     def to_custom_field_tag(field_type, options = {})         options = options.stringify_keys         to_input_field_tag(field_type, options)       end end  actionview::helpers::formbuilder.class_eval     def custom_field(method, options = {}, html_options = {})         @template.custom_field(@object_name, method, objectify_options(options), html_options)     end end  # form.custom_field helper use in views def custom_field(object_name, method, options = {}, html_options = {})     actionview::helpers::instancetag.new(object_name, method, self, options.delete(:object)).to_custom_field_tag(options.delete(:type), options) end  # form.custom_field_tag helper use in views def custom_field_tag(name, value = nil, options = {})     options = options.stringify_keys     # potential sanitation. taken rails3 code number_field     if range = options.delete("in") || options.delete("within")       options.update("min" => range.min, "max" => range.max)     end     text_field_tag(name, value, options) end 

then use in views:

<% form_for... |form| %>     <%= form.custom_field :user_age, :type=>"number", :min=>"0", :max=>"1000" %>     <%= form.custom_field :email, :type=>"email", :required=>"true" %> <% end %> 

which generate <input type='number', , <input type='email'

if have custom form builder, need expand/override well. namespace may vary, standard this:

myspecialformbuilder.class_eval     def custom_field(method, options = {}, html_options = {})         ...custom form builder implementation     end end 

Comments

Popular posts from this blog

assembly - 8086 TASM: Illegal Indexing Mode -

Java, LWJGL, OpenGL 1.1, decoding BufferedImage to Bytebuffer and binding to OpenGL across classes -

javascript - addthis share facebook and google+ url -