无法使用 SimpleForm 为某些(文本、布尔值等)类型创建自定义输入
我不明白为什么这不能正常工作 - 或者 - 我错过了一些重要的东西?
以下是 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
%>
当然,描述有一个文本数据类型。
我做错了什么?
谢谢。
来回
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
那么,要使用此功能,您只需要 1.5.1 版本的 SimpleForm。 :-)
Well, to use this functionality you just need the 1.5.1 version of SimpleForm. :-)