gpt4 book ai didi

ruby-on-rails - 带有自定义 rails 表单生成器的额外字段

转载 作者:数据小太阳 更新时间:2023-10-29 07:06:57 26 4
gpt4 key购买 nike

我有一个自定义表单生成器,使用此自定义生成器的原因之一是对于每个表单,我都需要包含一些额外的参数,我不想在每个表单中使用隐藏字段标签显式放入这些参数写。

for_for(@foo, :builder => MyBuilder) do |f|
# stuff I shouldn't have to worry about
# this should be put in all the time without me having to do it
hidden_field_tag('extra', 'myextrainfo')

# normal things I would put in
f.text_field(:bar)
end

我必须在我的自定义表单构建器中做什么,或者我可以覆盖什么或方法链以在表单中添加一些额外的隐藏内容(而不仅仅是添加 URL 参数)?

最佳答案

这有点棘手(对 Ruby/Rails 来说是新手),但我找到了解决方案。将它放在一些帮助文件中(或其他位置,具体取决于您的需要)。

module ActionView::Helpers::FormHelper
def form_for_with_updated_code(record_or_name_or_array, *args, &proc)
form_for_without_updated_code(record_or_name_or_array, *args) { |f|
concat(hidden_field_tag('extra','info'))
proc.call(f)
}
end
alias_method_chain :form_for, :updated_code
end

它会覆盖 form_for 方法并添加您的隐藏字段。您可以使用 extract_options!*args 参数向额外的个人选项添加代码,例如填充隐藏字段。

关于ruby-on-rails - 带有自定义 rails 表单生成器的额外字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2111718/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com