ActiveScaffold complains about association
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/cons开发者_如何转开发ole.
What gives?
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)
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
精彩评论