我有一个文本字段,我预先填充了一些文本,但是我发现如果表单验证失败,那么我添加的任何额外文本都不会持久化。
f.input :description, as: :text, input_html: { rows: 10, cols: 10, value: bike_description }
def bike_description
"text here"
end因此,如果我在文本字段中添加并读取text here and some more text,在表单验证失败时,该字段将只读取text here。
我如何才能让它记住我添加的任何额外文本,或者我会以另一种方式加载默认文本?
我试过把这个方法放到我的模型中。
def bike_description
read_attribute(:description).presence || 'text here'
end但我得到
undefined local variable or method `bike_description' for #<ActiveAdmin::Views::ActiveAdminForm:0x007fe9cb2d13a8>谢谢
发布于 2016-09-09 07:43:34
当前,您使用bike_description方法的返回值作为表单字段的值。无论如何在模型上设置描述,都将显示bike_description。
假设您的数据库有一个description,那么可以通过在模型中添加这样的方法来向属性读取器添加默认文本:
# remove the overwritten value getter from the form
f.input :description, as: :text, input_html: { rows: 10, cols: 10 }
# add this to your model
def description
read_attribute(:description).presence || 'text here'
end这将返回description属性的当前值,如果description文本为空,则返回默认文本。
https://stackoverflow.com/questions/39406202
复制相似问题