我有应用程序轨道并使用带有设计的 client_side_validations gem 来验证设计注册表单
//account.rb
has_one :user
validates :username, presence: true, uniqueness: true
//用户.rb
belongs_to :account
validates :email, presence: true, uniqueness: true
//new.html.erb
<%= form_for(@user, :url => registration_path(resource_name), :validate => true) do |f| %>
<%= f.fields_for :account, :validate => true do |inner_form|%>
<div class="field">
<%= inner_form.label :username, "Username" %><br />
<%= inner_form.text_field :username %>
</div>
<% end %>
<div class="field">
<%= f.label :email, "Email" %>
<%= f.email_field :email %>
</div>
<div>
<%= f.submit "Sign up" %>
</div>
<% end %>
验证在电子邮件字段中正常工作,但对于用户名不起作用..帮助我,,,谢谢
更新--- 验证显示,但这不是嵌套形式
将 :account 更改为 @account
<%= f.fields_for :account, :validate => true do |inner_form|%>
至
<%= f.fields_for @account, :validate => true do |inner_form|%>
提交时,我得到了预期的错误 Account(#27129348),得到了 ActiveSupport::HashWithIndifferentAccess(#26695068)
当验证不起作用时,html代码
<input id="user_account_attributes_username" name="user[account_attributes][username]" size="30" type="text" data-validate="true">
验证工作
<input id="user_account_username" name="user[account][username]" size="30" type="text" data-validate="true">
问题
user_account_attributes_username
用户[account_attributes][用户名]
工作过
user_account_username
用户[帐户][用户名]