表单验证失败后将数据保留在文本字段中 - Active Admin
Persist data in text field after form validation fails - Active Admin
我有一个文本字段,我预先填充了一些文本,但我发现如果表单验证失败,那么我添加的任何额外文本都不会保留
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>
谢谢
目前您使用 bike_description
方法的 return 值作为表单字段的值。无论在模型上如何设置描述,都会显示 bike_description
。
假设您的数据库有一个 description
,那么您可以通过向模型添加这样的方法来向属性 reader 添加默认文本:
# 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
这将 return description
属性的当前值或默认文本(如果 description
文本为空白)。
我有一个文本字段,我预先填充了一些文本,但我发现如果表单验证失败,那么我添加的任何额外文本都不会保留
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>
谢谢
目前您使用 bike_description
方法的 return 值作为表单字段的值。无论在模型上如何设置描述,都会显示 bike_description
。
假设您的数据库有一个 description
,那么您可以通过向模型添加这样的方法来向属性 reader 添加默认文本:
# 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
这将 return description
属性的当前值或默认文本(如果 description
文本为空白)。