ActiveScaffold 抱怨关联

发布于 2024-08-22 16:43:01 字数 295 浏览 5 评论 0原文

我正在使用 ActiveScaffold 创建管理 UI。

  • 我有两个模型:帖子和评论。
  • 一个帖子有很多评论,一条评论属于一个帖子。
  • 有一个 validates_presences_of :text 验证 Comment 模型。

问题是,当我从管理 UI 创建新帖子而没有在子表单中创建新评论时,ActiveScaffold 会抱怨验证。如果我还创建与之关联的评论,我就可以创建该帖子,但这不是我想要的。

我可以从脚本/控制台手动创建新帖子。

什么给?

I'm using ActiveScaffold to create an Admin UI.

  • I have two models: Post and Comments.
  • A Post has-many Comments, and a Comment belongs-to a post.
  • There's a validates_presences_of :text validation the Comment model.

The problem is that when I create a new Post from the Admin UI without creating a new Comment in the subform, ActiveScaffold complains about the validation. I can create the Post if I also create a Comment associated with it, which is not what I Want.

I can create a new Post manually from script/console.

What gives?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜声 2024-08-29 16:43:01

也许 ActiveScaffold 的东西你想为每个帖子至少创建一个评论。我在 has-one 中遇到过这个问题……看起来 ActiveScaffold 在 has-many 情况下足够聪明,但谁知道呢。

以下是我如何解决 has-one 的问题(这也是我想要的用户体验):

# if post has-one attachment
active_scaffold :post do |config|
  config.columns[:attachment].form_ui = :select
end

当然 :select 对注释没有意义,但您可以研究类似的 UI 更改,告诉它不要尝试坚持表单内联(如果确实发生了这种情况)

Maybe ActiveScaffold things that you want to create at least one Comment per Post. I've encountered this problem with a has-one… it seems like ActiveScaffold would be smart enough in the has-many case, but who knows.

Here's how I solved it for has-one (and is the UX I wanted anyway):

# if post has-one attachment
active_scaffold :post do |config|
  config.columns[:attachment].form_ui = :select
end

of course :select won't make sense for comments, but you could look into a similar UI change, telling it to not try to stick the form inline (if that is indeed what's happening)

寂寞美少年 2024-08-29 16:43:01

您希望在创建/编辑帖子时防止默认情况下(尝试)创建空白评论记录。幸运的是,AS 有一个 API::Column.show_blank_records 选项来控制此行为:

active_scaffold :post do |config|
  config.columns[:comments].show_blank_records = false
end

这将要求用户在创建/编辑帖子时单击“添加新”按钮来创建新的评论记录,因此不会进行验证检查在空白记录上运行。

请参阅 https://github.com/activescaffold/active_scaffold/wiki/API:-Column< /a>

You want to prevent the (attempted) creation of a blank Comment record by default when creating/editing a Post. Luckily AS has an API::Column.show_blank_records option to control this behavior:

active_scaffold :post do |config|
  config.columns[:comments].show_blank_records = false
end

This will require the user to click the 'Add New' button to create a new Comment record when creating/editing a Post, so the validation check won't be run on a blank record.

See https://github.com/activescaffold/active_scaffold/wiki/API:-Column

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文