ruby-on-rails - 在 Rails 5.2 应用程序中使用 Postgres 数组列时清除表单字段的最佳实践?
问题描述
我正在构建一个 Rails 5.2 应用程序,它有一个 Postgres 数组列来在 Post 模型上存储标签。我创建的迁移看起来像这样......
class AddTagsToPosts < ActiveRecord::Migration[5.2]
def change
change_table :posts do |t|
t.string :tags, array: true, default: []
end
end
end
由于需要设置默认值(?...我未能找到一种方法让 PG 数组列工作而不将标签的默认值设置为空数组),删除默认值的最佳做法是什么( []) 在不使用 JS 的情况下自动填写 Rails 新/编辑表单?
解决方案
<% 3.times do |num| %>
<%= form.text_field :tags, multiple: true, class: "form-control", value: @post.tags[num] %>
<% end %>
推荐阅读
- html - 如何将 html 传单地图插入博客文章(雨果网站)?
- java - 使用 Gradle 对 Sonarqube 8 的 Jacoco 代码覆盖率
- c++ - 我可以安全地 memset 一个具有用户定义构造函数的非平凡 C++ 结构吗?
- python - Python - 使用 For 循环打印数组
- python - 预测多项式值
- unetstack - 使用 JSON 远程访问 Unet 调制解调器
- html - 图片未在 Iphone 上显示,但在 dekstop 或 Android 手机上显示
- python - TensorFlow 教程 训练模型的图像少于预期
- ruby-on-rails - Ruby Net::IMAP - 带有特殊字符/变音符号的附件文件名
- python - 用 HTML 字符串提取 HTML 表 - Python / Beautiful Soup