【发布时间】:2015-12-01 17:43:13
【问题描述】:
我是Ruby 的新手,使用ActiveAdmin 做了一些简单的管理员。
我有一个模型Question,我想创建、填充并存储到数据库中,它有一个属性themes(Theme 模型数组)。当用户创建新记录时,他不会手动输入主题,而是提供一些字符串,系统会自动解析并查找或创建主题。所以我有这样的代码:
form do |f|
f.inputs "Questions Details" do
f.input :question, as: :string
f.input :autocomplete_themes, hint: "You should enter here multiple themes,
divide them with `,` or `;`"
end
f.actions
end
它创建了一个新字段autocomplete_themes 用于输入字符串,并且它不存在于模型Question 中。所以我想要的 - 是获取 autocomplete_themes 类似字符串的值,然后使用 split() 和我的自定义逻辑 - 但它给出了一个错误。
before_create do |question|
array = []
puts "******"
puts :autocomplete_themes.text
themeTitles = :autocomplete_themes.split(",") #split(/,|;/)
for title in themeTitles do
theme = Theme.find_by(title: title)
theme = Theme.create(title: title) unless theme
array << theme
end
question.themes = array
end
问题:我怎样才能得到autocomplete_themes 值作为字符串?谢谢!
更新: 据我了解here - 看起来类似,但将默认值设置为自定义字段时出现问题,但我需要从代码中获取其值。
【问题讨论】:
标签: ruby-on-rails ruby forms activeadmin