0

我无法弄清楚为什么这不能正常工作 - 或者 - 我错过了一些重要的东西?

这是来自 simple_form 的映射类型列表/ lib / simple_form / form_builder.rb

map_type :text,                                :to => SimpleForm::Inputs::TextInput
map_type :file,                                :to => SimpleForm::Inputs::FileInput
map_type :string, :email, :search, :tel, :url, :to => SimpleForm::Inputs::StringInput
map_type :password,                            :to => SimpleForm::Inputs::PasswordInput
map_type :integer, :decimal, :float,           :to => SimpleForm::Inputs::NumericInput
map_type :range,                               :to => SimpleForm::Inputs::RangeInput
map_type :select, :radio, :check_boxes,        :to => SimpleForm::Inputs::CollectionInput
map_type :date, :time, :datetime,              :to => SimpleForm::Inputs::DateTimeInput
map_type :country, :time_zone,                 :to => SimpleForm::Inputs::PriorityInput
map_type :boolean,                             :to => SimpleForm::Inputs::BooleanInput

第一个问题,我可以将 StringInput 类扩展为:

#app/inputs/string_input.rb
class StringInput < SimpleForm::Inputs::StringInput
  def input
    if @builder.show?
      content_tag(:p, @builder.object[attribute_name], :class => :show)
    else
      super
    end
  end
end

(我用 show? 方法扩展了构建器来检测上下文:action == 'show')

这在大多数情况下都有效,但在:as => :string存在时无效:

<%= f.input :estimated_duration_rendered, 
  :as => :string,
  :label => mt(:estimated_duration), 
  :hint => mt(:estimated_duration_hint),
  :error => false,
  :required => false,
  :input_html => { :class => :digits_11, :placeholder => mt(:estimated_duration_placeholder), :value => format_duration(resource.estimated_duration, true) }
%>

第二个问题,我可以为 StringInput、DateTimeInput、CollectionInput 和 BooleanInput 创建自定义输入,但其他所有输入都不起作用。例如:

#app/inputs/text_input.rb
class TextInput < SimpleForm::Inputs::TextInput
  def input
    if @builder.show?
      "I will never show..."
    else
      super
    end
  end
end

即使我的表单中有这个助手:

<%= f.input :description, 
            :label => mt(:description), 
            :hint => mt(:description_hint, :max => MyModel::DESC_MAX_LENGTH), 
            :input_html => { :class => :large, :rows => 8, :cols => 1, :maxlength => MyModel::DESC_MAX_LENGTH, :placeholder => mt(:description_placeholder) }, 
            :error => false
%>

当然,description 具有文本数据类型。

我究竟做错了什么 ?

谢谢你。

来回

4

1 回答 1

2

好吧,要使用这个功能,您只需要 SimpleForm 的1.5.1版本。:-)

于 2011-09-16T14:26:02.837 回答