0

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?

Bilal and Olga
  • 3,211
  • 6
  • 30
  • 33

2 Answers2

0

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)

John Bachir
  • 22,495
  • 29
  • 154
  • 227
0

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

Ryan Barton
  • 313
  • 3
  • 12