开发者

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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜