DEFAULT_FIELD_OPTIONS

All,

I have spent the last 6hrs trying to change the default, size="30", that is returned as a result of f.text_field... helper call. I wanted the default width to be '16' for my application.

I am using Rails 3.0.3 and Ruby 1.8.7.

I have tried the following:

1. Adding this line in config/environment.rb:

ActionView::Helpers::InstanceTag::DEFAULT_FIELD_OPTIONS = { "size" => 16 }

After doing this, the console yields the following: irb(main):004:0> puts ActionView::Helpers::InstanceTag::DEFAULT_FIELD_OPTIONS["size"] 16 =>nil

additionally, the following line in a partial... <% ActionView::Helpers::InstanceTag::DEFAULT_FIELD_OPTIONS["size"] %>

produces a static '16' in the generated source

However... The following html source code is rendered in my views:     <input id="party_names_attributes_0_surname" name="party[names_attributes][0][surname]" size="30" type="text" value="SomeText" />

2. Next I created a plugin file 'vender/plugins/default_options/lib/ default_options.rb' (also created the requisite 'init.rb'):

module ActionView   module Helpers     class InstanceTag       DEFAULT_FIELD_OPTIONS = { "size" => 16 }     end   end end

This modification affects the value stored in ActionView::Helpers::InstanceTag::DEFAULT_FIELD_OPTIONS["size"] but not the results of f.text_field.

3. Finally I was able to accomplish the desired results by modifying my plugin as follows:

module ActionView   module Helpers     class InstanceTag       DEFAULT_FIELD_OPTIONS = { "size" => 16 }

      def to_input_field_tag(field_type, options = {})         options = options.stringify_keys         puts "here"         options["size"] = options["maxlength"] || DEFAULT_FIELD_OPTIONS["size"] unless options.key?("size")         options = DEFAULT_FIELD_OPTIONS.merge(options)         if field_type == "hidden"           options.delete("size")         end         options["type"] ||= field_type         options["value"] = options.fetch("value") { value_before_type_cast(object) } unless field_type == "file"         options["value"] &&= ERB::Util.html_escape(options["value"])         add_default_name_and_id(options)         tag("input", options)       end     end   end end

Surely I am missing something. What is the 'one-line' trick to setting the default size= option for a text_field?

Thanks

Looking at the source, it would appear that the existing DEFAULT_FIELD_OPTIONS is in ActionView::Helpers::InstanceTagMethods not ActionView::Helpers::InstanceTag (or was the above a typo?)

Fred

Thanks for looking at this Fred. You are correct. DEFAULT_FIELD_OPTIONS is indeed defined in InstanceTagMethods. However...

below the module InstanceTagMethods class InstanceTag is defined as follows:

class InstanceTag   include InstanceTagMethods end

I actually located it after some searching on google and using the tab- completion in console to arrive at: ActionView::Helpers::InstanceTag::DEFAULT<tab> ... ActionView::Helpers::InstanceTag::DEFAULT_FIELD_OPTIONS ActionView::Helpers::InstanceTag::DEFAULT_RADIO_OPTIONS ActionView::Helpers::InstanceTag::DEFAULT_TEXT_AREA_OPTIONS

Redefining ActionView::Helpers::InstanceTagMethods::DEFAULT_FIELD_OPTIONS in either environment.rb or in vendor/plugins/default_options/lib/ default_options.rb effects the change that I am looking for but in both cases I get warning like the following...

work/rails/codata/vendor/plugins/default_options/lib/ default_options.rb:4: warning: already initialized constant DEFAULT_FIELD_OPTIONS or work/rails/codata/config/environment.rb:5: warning: already initialized constant DEFAULT_FIELD_OPTIONS

If I 'modify the constant' as shown in my original post I don't get these warnings.

work/rails/codata/vendor/plugins/default_options/lib/ default_options.rb:4: warning: already initialized constant DEFAULT_FIELD_OPTIONS or work/rails/codata/config/environment.rb:5: warning: already initialized constant DEFAULT_FIELD_OPTIONS

If I 'modify the constant' as shown in my original post I don't get these warnings.

Your original method was just creating some random new constant. Now you are actually redefining the constant, so you get a warning about it (There's a silence_warning method)

Fred