您好我正在尝试设置一个仅在特定表单视图中调用的验证,为此我正在尝试为表单上的虚拟属性创建一个 hidden_field 并将其设置为一个值,然后验证:如果虚拟属性等于值。
到目前为止,我有:
## user model
validates_presence_of :password_confirmation, :if => :confirmation_validation
attr_accessible :email, :password, :password_confirmation,
:remember_me, :name, :avatar, :username, :bio, :confirmation_validation
def confirmation_validation
# not sure what goes here???
end
## form view
<%= form_for(resource, :validate => true, :as => resource_name, :url => registration_path(resource_name), :html => { :method => :put }, :html => {:multipart => true}) do |f| %>
<%= devise_error_messages! %>
<p><%= f.label :password %> <i>(leave blank if you don't want to change it)</i><br />
<%= f.password_field :password %></p>
<p><%= f.label :password_confirmation %><br />
<%= f.password_field :password_confirmation %>
<% f.hidden_field :confirmation_validation, :value => 100%></p>
<p><%= f.submit "Update" %></p>
<% end %>