Ruby 如何编写自定义SimpleForm Builder以替换<;输入>;由<;P>;?
我的文件摘录:Ruby 如何编写自定义SimpleForm Builder以替换<;输入>;由<;P>;?,ruby,ruby-on-rails-3,customization,simple-form,formbuilder,Ruby,Ruby On Rails 3,Customization,Simple Form,Formbuilder,我的文件摘录: gem 'rails', '3.0.3' gem 'inherited_resources', '1.2.1' gem 'simple_form', '1.4.0' 对于任何资源,我有3个操作(新建、编辑和显示)的1个视图。例如: controller\u-friendly\u-name)%%> “头衔”, :hint=>I18n.t('admin.form.input.title.hint',:name=>controller\u-friendly\u-name), :req
gem 'rails', '3.0.3'
gem 'inherited_resources', '1.2.1'
gem 'simple_form', '1.4.0'
对于任何资源,我有3个操作(新建、编辑和显示)的1个视图。例如:
controller\u-friendly\u-name)%%>
“头衔”,
:hint=>I18n.t('admin.form.input.title.hint',:name=>controller\u-friendly\u-name),
:required=>true,
:error=>false,
:input\u html=>{:class=>:large,:placeholder=>I18n.t('admin.form.input.title.placeholder',:name=>controller\u-friendly\u-name)}
%>
:收音机,
:label=>“可见”,
:error=>false,
:required=>true,
:collection=>[['Yes',true],'No',false]],
:wrapper_class=>“复选框和单选按钮”,
:checked=>true
%>
f%>
查看#显示操作如何将所有字段设置为禁用?这太难看了。考虑到我不能重构视图以显示一个.HTML.Erb文件。 我想做的是: 当动作为“显示”时,简单表单生成器使用一个自定义生成器,用值替换
,
,
,
此外,我将定制单选按钮,复选框,以
我的第一步:
#app/inputs/showvalue_input.rb
类ShowvalueInput
找不到做这件事的方法。自定义表单生成器或自定义输入(使用猴子补丁)
谢谢你的帮助 这是我的解决办法
在我的应用程序_helper.rb中:
def set_show_method_to_builder(builder)
builder.instance_eval <<-EVAL
def show?
#{action_name == "show"}
end
EVAL
end
def set_show_method_to_builder(builder)
builder.instance\u eval
<%- set_show_method_to_builder(f) -%>
class StringInput < SimpleForm::Inputs::StringInput
def input
if @builder.show?
content_tag(:p, @builder.object[attribute_name], :class => :show)
else
super
end
end
end