Can't create Custom inputs for some (Text, Booleans, …) types, with SimpleForm

百般思念 提交于 2019-12-23 05:43:11

问题


I can't figure out why this is not working as it should - or - I'm missing something important ?

Here's the list of the mapped types from 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

First problem, I can extend StringInput class as:

#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

(I've extended the builder with a show? method to detect the context: action == 'show')

This is working most of the time but not when :as => :string is present :

<%= 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) }
%>

Second problem, I can create custom inputs for StringInput, DateTimeInput, CollectionInput and BooleanInput but all the others are not working. For example:

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

Even if I have this helper in my form:

<%= 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
%>

Of course, description has a text data type.

What Am I doing wrong ?

Thank you.

Fro


回答1:


Well, to use this functionality you just need the 1.5.1 version of SimpleForm. :-)



来源:https://stackoverflow.com/questions/7435541/cant-create-custom-inputs-for-some-text-booleans-types-with-simpleform

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!