gpt4 book ai didi

ruby-on-rails - ActiveAdmin:没有对应表属性的表单输入

转载 作者:行者123 更新时间:2023-12-01 03:29:17 25 4
gpt4 key购买 nike

如何在 ActiveAdmin 表单中设置与表属性不对应的文本字段?

我需要它来构建自动完成行为,以填充复选框列表。

最佳答案

如果您希望将值提交回您的模型,您可以通过添加 attr_accessible 创建一个虚拟属性。 ,或在模型中明确定义它们,如此 answer 中所建议的:

def my_virtual_attr= (attributes)
#this will be evaluated when you save the form
end

def my_virtual_attr
# the return of this method will be the default value of the field
end

并且您需要将其添加到 permit_params在 ActiveModel 资源文件中。

如果您不需要提交给后端的值(例如前端处理需要),您实际上可以将任何自定义 HTML 添加到 ActiveAdmin form ,这是一个示例代码:
ActiveAdmin.register MyModel do
form do |f|
f.semantic_errors # shows errors on :base
f.inputs "My Custom HTML" do
f.li "<label class='label'>Label Name</label><a class='js-anchor' href='#{link}'>Link Text</a><span></span>".html_safe
f.li "<label class='label'>Label 2 Name</label><input id='auto_complete_input'/>".html_safe
end
f.inputs "Default Form Attributes" do
f.inputs # builds an input field for every attribute
end
f.actions # adds the 'Submit' and 'Cancel' buttons
end
end

关于ruby-on-rails - ActiveAdmin:没有对应表属性的表单输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39153004/

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