accepts_nested_attribute minimum
How can I enforce a min number of nested attributes or associated records during creation of the Model. This works fine but the error message is shown at the top of the form (nested), I want it to appear inline or near the nested association fields.
Model Vote
has_many :vote_items, :dependent => :destroy
validate :min_vote_items, :if => :its_new?
accepts_nested_attributes_for :vote_items, :limit => 5, :allow_destroy => true, :reject_if => proc { |attrs| attrs[:开发者_如何转开发option].blank? }
def min_vote_items
if self.vote_items.length < 2
errors.add_to_base("Please specify at least two vote options")
return false
end
end
Hi You may use something like
<h5>
<%= @user.errors.on(:name)%>
</h5>
<p>
<%= f.label :name %><br />
<%= f.text_field :name %>
</p>
精彩评论