Rails 表单不保存“类型:"字段 [英] Rails form not saving 'Type:' field

查看:37
本文介绍了Rails 表单不保存“类型:"字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我生成了一个简单的 Post 脚手架,它具有 title:string body:text category:string.我后来在模型中添加了 type:string(并执行了迁移),并在 new.html.erb 和 edit.html.erb 中添加了选择字段.我还为所有这些字段添加了验证.

I generated a simple Post scaffold which has title:string body:text category:string. I later added type:string (and performed the migration) to the model and added the selection fields in new.html.erb and edit.html.erb. I also added validation for all these fields.

<%= f.label :type %>
<%= f.select :type, Post::TYPES, :prompt => "Select post type" %>

当我尝试创建帖子时,它给了我:

When I try and create a post it gives me:

以下字段存在问题:类型不能为空类型未包含在列表中"

"There were problems with the following fields: Type can't be blank Type is not included in the list"

即使我做了选择.我在这里遗漏了一些明显的东西吗?

Even though I DO make a selection. Am I missing something obvious here?

从 Post 类中选择代码:

Select code from Post class:

TYPES = [
  ["Job", "job"],
  ["Volunteer", "vol"]
]

validates_presence_of :title, :body, :category, :type
validates_inclusion_of :category, :in => CATEGORIES.map {|disp, value| value}
validates_inclusion_of :type, :in => TYPES.map {|disp, value| value}

推荐答案

type 字段是用于单表继承 (STI) 的保留字段.您必须重命名该字段.

The type field is a reserved field used for single table inheritance(STI). You have to rename the field.

有关详细信息,请参阅这篇文章

Refer to this article for more details

将链接更改为指向 Matchu 提供的文章.

Changed the link to point to the article provide by Matchu.

这篇关于Rails 表单不保存“类型:"字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆