我尝试验证属于另一个模型的属性的存在,但无济于事。
具有
使用此表格:
= simple_form_for(resource, :as => resource_name, :html => { :class => 'form-horizontal' } , :validate => true , :url => registration_path(resource_name)) do |f|
= f.input :username, :label => t(:username)
= f.input :email, :label => t(:email),
:hint => t(:hint_email_visible)
= f.input :password, :label => t(:password), :require => true
= f.input :password_confirmation, :label => t(:password_confirm)
- resource.build_profile
= f.fields_for :profile do |f|
#render
= f.hidden_field :form, :value => "signup"
.clear
= f.input :gender,
:label => t(:your_gender),
:collection => gender,
:item_wrapper_class => 'inline',
:as => :radio_buttons
.clear
= f.button :submit, t(:submit, :scope => :register) + " »"
如何通过上述表单设置验证性别字段的存在?
答案 0 :(得分:1)
以下代码可以帮助您。
控制器/ users_controller.rb
class UsersController < ApplicationController
def new
@user = User.new
@user.build_profile
end
end
模型/ users.rb的
class User < ActiveRecord::Base
has_one :profile, dependent: :destroy
accepts_nested_attributes_for :profile, :allow_destroy => true
end
模型/ profile.rb
class Profile < ActiveRecord::Base
validates_presence_of :gender
end
可能需要从视图中删除以下行。
- resource.build_profile
以下是您认为有帮助的链接
答案 1 :(得分:-1)
虽然以上是正确答案,而且我已经像这样实施了,但性别领域的情况似乎仍在验证。所以仍然有同样的问题。是否有可能重新打开这个问题?