ibl*_*lue 0 ruby-on-rails simple-form
我想建立一个自定义表单生成器中simple_form,里面加一些隐藏字段的形式,而不使用form.hidden_field.我注意到,utf8和authenticity_token隐藏字段会自动添加到每个表单.
是否有类似的机制来添加另一个自定义隐藏字段,但仅限于由我的自定义表单生成器生成的表单?
您可以将其集成到自定义输入中,而不是在FormBuilder级别修补它:
class MagicInput < SimpleForm::Inputs::HiddenInput
def input
if object.condition?
@builder.hidden_field(:hidden_field_name, value: "some value").html_safe
# You could also call #super here (instead of the previous call)
# because this extends HiddenInput (might be cleaner, depending on what you
# want to achieve)
end
end
end
Run Code Online (Sandbox Code Playgroud)
只有在object.condition?为true时,此自定义输入才会在表单中注入隐藏字段.显然,您需要#condition?在传递给表单的对象上创建方法(或者用wathever替换此条件行浮动您的船).
然后在你的视图中你会称之为:
= f.input :something, as: :magic
Run Code Online (Sandbox Code Playgroud)
你的隐藏场只会在object.condition?传球时出现.
编辑:对于多汁的detials - 实现utf8和authenticity_token隐藏的字段form_tag- 不是在FormBuilder中实现的:https://github.com/rails/rails/blob/801e159ce2f4645f6839c94ab0febf97a0d8543d/actionview/lib/action_view/helpers/form_tag_helper.rb# L713
| 归档时间: |
|
| 查看次数: |
380 次 |
| 最近记录: |