1

当我提交样式创建表单时,我想创建一个具有一些功能的新样式。这些特征是从一个多选框中选择的,该框是从数据库中的特征表中填充的。根据从多选中选择的功能,直通表应适当更新。

我可以在 Rails 控制台中执行以下操作:

@style = Style.first
@style.feature_ids = ['','2','4','6']

输入上述内容后,我将看到我的直通表更新为三行适当的 style_id 和 feature_id。

在我的样式创建表单中,我有以下内容:

<%= m.input :feature_ids, :label => "Features", :collection => @features, :input_html => { :multiple => true } %>

提交表单后,我收到以下 PG 错误:

PG::Error: ERROR:  column "feature_id" is of type integer but expression is of type character varying at character 96
HINT:  You will need to rewrite or cast the expression.
: INSERT INTO "stylefeatures" ("created_at", "feature_id", "style_id", "updated_at") VALUES ($1, $2, $3, $4) RETURNING "id"

从参数哈希:

{"utf8"=>"✓",
 "authenticity_token"=>"lkdI9jhlhPW1P2Tyb8jFMKFM/kVXvgcJfSkL0qNH7xk=",
 "style"=>{"name"=>"123",
 "feature_ids"=>["",
 "2",
 "4",
 "6"],
 "commit"=>"Create Style"}
4

1 回答 1

1

@features是字符串的集合,将其更改为整数,您会很好(这就是您的数据库所抱怨的)。

于 2012-05-22T09:34:48.063 回答