简体   繁体   English

将rails 3表单助手添加到rails 2

[英]Adding rails 3 form helpers to rails 2

I would like to add the number_field form helper that exists in rails 3 to my rails 2.3.15 app, but i'm having trouble extending the module. 我想将Rails 3中存在的number_field表单助手添加到我的rails 2.3.15应用程序中,但是我在扩展模块时遇到了麻烦。

These are the methods I need from rails 3 这些是我从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 this to a module which i include in my application helper. 我将其添加到我的应用程序帮助程序中包含的模块中。 The to_number_field_tag method is easy because i can just open the class and add the override. to_number_field_tag方法很简单,因为我可以打开类并添加覆盖。

The FormHelper module methods I'm having trouble with because i can't quite figure out the ancestors chain and don't know how to scope my override. 我遇到麻烦的FormHelper模块方法,因为我不太清楚祖先链,也不知道如何确定覆盖范围。 I don't know how to make it work basically. 我不知道该如何使其基本工作。

My problem above was that i was not overriding the FormBuilder. 我上面的问题是我没有重写FormBuilder。 Here is a solution for those who might need this in the future. 这是针对将来可能需要此解决方案的人的解决方案。

Rather than just implementing the type="number" input type, i decided to make a generic helper for all new HTML5 inputs. 我决定不只是实现type="number"输入类型,而是决定为所有新的HTML5输入创建通用帮助器。 I place this code in an overrides file which i include in application_helper.rb . 我将此代码放在我包含在application_helper.rb中的替代文件中。

# file 'rails_overrides.rb`

ActionView::Helpers::InstanceTag.class_eval do
    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 do
    def custom_field(method, options = {}, html_options = {})
        @template.custom_field(@object_name, method, objectify_options(options), html_options)
    end
end

# form.custom_field helper to 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 to use in views
def custom_field_tag(name, value = nil, options = {})
    options = options.stringify_keys
    # potential sanitation. Taken from rails3 code for 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 to use this in your views: 然后在您的视图中使用它:

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

Which will generate an <input type='number', and an <input type='email' 这将生成一个<input type='number', and an <input type='email'

If you have a custom form builder, you will need to expand/override that as well. 如果您有一个自定义表单构建器,则还需要扩展/覆盖它。 Namespace may vary, but most standard is like this: 命名空间可能有所不同,但大多数标准是这样的:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM