无法使用SimpleForm为某些(文本,布尔值,...)类型创建自定义输入 [英] Can't create Custom inputs for some (Text, Booleans, ...) types, with SimpleForm
问题描述
我不知道为什么它不能正常工作-或-我缺少重要的东西吗?
I can't figure out why this is not working as it should - or - I'm missing something important ?
这是来自simple_form /lib/simple_form/form_builder.rb
的映射类型的列表:
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
第一个问题,我可以将StringInput类扩展为:
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
(我已使用show?方法扩展了构建器以检测上下文:action =='show')
大多数时间都可以使用,但是:as =>不能使用:string
存在:
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) }
%>
第二个问题,我可以为StringInput,DateTimeInput,CollectionInput和BooleanInput创建自定义输入,但所有其他输入均不起作用.例如:
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
%>
当然,描述具有文本数据类型.
我在做什么错了?
谢谢.
来回
推荐答案
好,要使用此功能,您只需要 1.5.1 版本的SimpleForm.:-)
Well, to use this functionality you just need the 1.5.1 version of SimpleForm. :-)
这篇关于无法使用SimpleForm为某些(文本,布尔值,...)类型创建自定义输入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!