1

I have a select form in Rails and currently I am using a list for the form in my view and Profile model:

class Profile < ActiveRecord::Base
     validates :country, inclusion: { :in => CITYLIST }
     CITYLIST = %w[LA NYC Miami Boston]
end

In my view,

<%= f.select(:city, Profile::CITYLIST, {:include_blank => "Select"}, {:class => "editstyle"}) %>

Currently, the user has to select a value from the city list before his profile is saved. What I'd like is that the user need not select a value from the list. As in he can save his profile by leaving the value blank, but if he chooses to select a value, only then the value must be validated from the list.

4

1 回答 1

3

将此添加到您的验证中:

:allow_blank => true
于 2013-05-11T05:19:45.993 回答