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