首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >表单验证失败后将数据持久化到文本字段中- Active Admin

表单验证失败后将数据持久化到文本字段中- Active Admin
EN

Stack Overflow用户
提问于 2016-09-09 07:34:21
回答 1查看 189关注 0票数 0

我有一个文本字段,我预先填充了一些文本,但是我发现如果表单验证失败,那么我添加的任何额外文本都不会持久化。

代码语言:javascript
复制
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

我如何才能让它记住我添加的任何额外文本,或者我会以另一种方式加载默认文本?

我试过把这个方法放到我的模型中。

代码语言:javascript
复制
def bike_description
  read_attribute(:description).presence || 'text here'
end

但我得到

代码语言:javascript
复制
undefined local variable or method `bike_description' for #<ActiveAdmin::Views::ActiveAdminForm:0x007fe9cb2d13a8>

谢谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-09 07:43:34

当前,您使用bike_description方法的返回值作为表单字段的值。无论如何在模型上设置描述,都将显示bike_description

假设您的数据库有一个description,那么可以通过在模型中添加这样的方法来向属性读取器添加默认文本:

代码语言:javascript
复制
# 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文本为空,则返回默认文本。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39406202

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档