Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ruby-on-rails-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails 严重卡在打开状态:未为字段为“”的嵌套资源触发验证_Ruby On Rails_Ruby On Rails 3_Validation_Activerecord_Nested Attributes - Fatal编程技术网

Ruby on rails 严重卡在打开状态:未为字段为“”的嵌套资源触发验证

Ruby on rails 严重卡在打开状态:未为字段为“”的嵌套资源触发验证,ruby-on-rails,ruby-on-rails-3,validation,activerecord,nested-attributes,Ruby On Rails,Ruby On Rails 3,Validation,Activerecord,Nested Attributes,我在一个问题上陷入了困境,因为我有一个用户和配置文件模型。 无论我尝试什么,似乎都不会为任何配置文件属性触发验证 配置文件模型属于用户。用户有一个配置文件 在注册表上使用字段_显示一些个人资料字段(性别、城市和国家) 无论我做了什么尝试,我都无法让性别和城市(profile字段的字段_)进行验证 我的表格: - title t(:title, :scope => :register) %h1= yield(:title) = simple_form_for(resource, :

我在一个问题上陷入了困境,因为我有一个用户和配置文件模型。 无论我尝试什么,似乎都不会为任何配置文件属性触发验证

  • 配置文件模型属于用户。用户有一个配置文件
  • 在注册表上使用字段_显示一些个人资料字段(性别、城市和国家)
  • 无论我做了什么尝试,我都无法让性别和城市(profile字段的字段_)进行验证
我的表格:

- title t(:title, :scope => :register)
%h1= yield(:title)

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

      #div

        = f.hidden_field :form, :value => "signup"

        .clear

        = f.input :gender,                :collection => [['Male', 'male'], ['Female', 'female']],
                                          :as => :radio

    = f.input :city,
              :readonly => "readonly",
              :label => t(:city)

    .ruler

    = f.input :country,
              :label => "Your country",
              :collection => [DataCountry.where(:code => 155).first],
              :value => @city,
              :id => "country",
              :name => "country"

    .clear


    = f.button :submit, t(:submit, :scope => :register) + " »"
  accepts_nested_attributes_for :profile
  validates_presence_of :gender
  validates_presence_of :city
  validates_presence_of :country
-- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
authenticity_token: <mytokenhere>
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  invite_code: ''
  username: username
  email: ''
  password: ''
  password_confirmation: ''
  profile_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    form: signup
    gender: male
    dob(3i): '9'
    dob(2i): '7'
    dob(1i): '1942'
    city: 'somevalue'
    country: 'somevalue'
commit: Register »
action: create
controller: registrations
- resource.build_profile
- resource.build_profile unless resource.profile
我的用户模型有以下内容:

- title t(:title, :scope => :register)
%h1= yield(:title)

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

      #div

        = f.hidden_field :form, :value => "signup"

        .clear

        = f.input :gender,                :collection => [['Male', 'male'], ['Female', 'female']],
                                          :as => :radio

    = f.input :city,
              :readonly => "readonly",
              :label => t(:city)

    .ruler

    = f.input :country,
              :label => "Your country",
              :collection => [DataCountry.where(:code => 155).first],
              :value => @city,
              :id => "country",
              :name => "country"

    .clear


    = f.button :submit, t(:submit, :scope => :register) + " »"
  accepts_nested_attributes_for :profile
  validates_presence_of :gender
  validates_presence_of :city
  validates_presence_of :country
-- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
authenticity_token: <mytokenhere>
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  invite_code: ''
  username: username
  email: ''
  password: ''
  password_confirmation: ''
  profile_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    form: signup
    gender: male
    dob(3i): '9'
    dob(2i): '7'
    dob(1i): '1942'
    city: 'somevalue'
    country: 'somevalue'
commit: Register »
action: create
controller: registrations
- resource.build_profile
- resource.build_profile unless resource.profile
带有验证的我的个人资料模型如下:

- title t(:title, :scope => :register)
%h1= yield(:title)

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

      #div

        = f.hidden_field :form, :value => "signup"

        .clear

        = f.input :gender,                :collection => [['Male', 'male'], ['Female', 'female']],
                                          :as => :radio

    = f.input :city,
              :readonly => "readonly",
              :label => t(:city)

    .ruler

    = f.input :country,
              :label => "Your country",
              :collection => [DataCountry.where(:code => 155).first],
              :value => @city,
              :id => "country",
              :name => "country"

    .clear


    = f.button :submit, t(:submit, :scope => :register) + " »"
  accepts_nested_attributes_for :profile
  validates_presence_of :gender
  validates_presence_of :city
  validates_presence_of :country
-- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
authenticity_token: <mytokenhere>
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  invite_code: ''
  username: username
  email: ''
  password: ''
  password_confirmation: ''
  profile_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    form: signup
    gender: male
    dob(3i): '9'
    dob(2i): '7'
    dob(1i): '1942'
    city: 'somevalue'
    country: 'somevalue'
commit: Register »
action: create
controller: registrations
- resource.build_profile
- resource.build_profile unless resource.profile
我的参数:

- title t(:title, :scope => :register)
%h1= yield(:title)

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

      #div

        = f.hidden_field :form, :value => "signup"

        .clear

        = f.input :gender,                :collection => [['Male', 'male'], ['Female', 'female']],
                                          :as => :radio

    = f.input :city,
              :readonly => "readonly",
              :label => t(:city)

    .ruler

    = f.input :country,
              :label => "Your country",
              :collection => [DataCountry.where(:code => 155).first],
              :value => @city,
              :id => "country",
              :name => "country"

    .clear


    = f.button :submit, t(:submit, :scope => :register) + " »"
  accepts_nested_attributes_for :profile
  validates_presence_of :gender
  validates_presence_of :city
  validates_presence_of :country
-- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
authenticity_token: <mytokenhere>
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  invite_code: ''
  username: username
  email: ''
  password: ''
  password_confirmation: ''
  profile_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    form: signup
    gender: male
    dob(3i): '9'
    dob(2i): '7'
    dob(1i): '1942'
    city: 'somevalue'
    country: 'somevalue'
commit: Register »
action: create
controller: registrations
- resource.build_profile
- resource.build_profile unless resource.profile
要查看到底发生了什么,将抛出以下错误列表:

Email can't be blank
Email can't be blank
Password can't be blank
Password can't be blank
Profile city can't be blank
Profile gender can't be blank
Profile is invalid
Username can't be blank
Password confirmation can't be blank 
看来验证已经被检查过了。。。。
但是没有应用错误样式!此外,配置文件无效????我不确定,但这是否是指检查数据库中是否有任何默认值。似乎在您在问题中粘贴的表格params中,性别传递了value
male
。即使您没有检查任何性别,此表单参数是否已通过?在这种情况下,数据库中的性别字段的默认性别值必须为
male
。在这种情况下,即使是一个新的
概要文件
模型对象也将其性别属性设置为
男性
,因此它将通过验证并将性别保存为数据库中的
男性
。例如,我在用户表中有一个布尔字段
is_admin
,并将其默认值设置为
false
0
。如果我这样做:

User.new
=>  #<User id: nil, name: nil, email: nil, is_admin: false>
User.new
=>  #

您可以看到
is_admin
属性设置为
false
,而不是
nil
。现在,如果我们没有为
is_admin
传递任何值,它将在数据库中保存为
false
,并且不会触发
is_admin
字段的验证,因为它存在于对象的属性中(值为
false
)。

首先,粘贴的参数包括配置文件的城市、国家和性别的值。如果这是准确的,那么验证应该通过,因为
概要文件
模型验证这3个字段的存在——它们是存在的

我还注意到,您正在视图中执行
resource.build\u profile
,这将建立属于用户的默认配置文件。如果在实例化期间在概要文件中分配了任何值,这将导致它们进入表单,最终进入params散列

因此,我猜想您正在沿着这条线填充字段,可能是在
User
Profile
模型中。您应该再次检查这些,尤其是在初始化新配置文件期间

根据你提供的数据,我只能猜到这些。如果您仍然找不到问题,您可以尝试将其提交到GitHub(如果您不愿意共享真实的应用程序,也可以尝试使用一个单独的应用程序复制问题),以便我们可以更直接地分析问题

编辑:刚刚注意到,你在《赏金》中写道:

任何人都可以提供嵌套资源和验证的工作示例


因此,请参阅,以获取一个简单的用户验证示例,该示例使用根据您的问题建模的配置文件的嵌套属性进行验证。它验证了
用户#电子邮件
个人资料#城市
个人资料#国家
个人资料#性别
,现在,解决方案非常简单,基于本主题中的建议,我发现

错误:

- title t(:title, :scope => :register)
%h1= yield(:title)

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

      #div

        = f.hidden_field :form, :value => "signup"

        .clear

        = f.input :gender,                :collection => [['Male', 'male'], ['Female', 'female']],
                                          :as => :radio

    = f.input :city,
              :readonly => "readonly",
              :label => t(:city)

    .ruler

    = f.input :country,
              :label => "Your country",
              :collection => [DataCountry.where(:code => 155).first],
              :value => @city,
              :id => "country",
              :name => "country"

    .clear


    = f.button :submit, t(:submit, :scope => :register) + " »"
  accepts_nested_attributes_for :profile
  validates_presence_of :gender
  validates_presence_of :city
  validates_presence_of :country
-- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
authenticity_token: <mytokenhere>
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  invite_code: ''
  username: username
  email: ''
  password: ''
  password_confirmation: ''
  profile_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    form: signup
    gender: male
    dob(3i): '9'
    dob(2i): '7'
    dob(1i): '1942'
    city: 'somevalue'
    country: 'somevalue'
commit: Register »
action: create
controller: registrations
- resource.build_profile
- resource.build_profile unless resource.profile
正确:

- title t(:title, :scope => :register)
%h1= yield(:title)

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

      #div

        = f.hidden_field :form, :value => "signup"

        .clear

        = f.input :gender,                :collection => [['Male', 'male'], ['Female', 'female']],
                                          :as => :radio

    = f.input :city,
              :readonly => "readonly",
              :label => t(:city)

    .ruler

    = f.input :country,
              :label => "Your country",
              :collection => [DataCountry.where(:code => 155).first],
              :value => @city,
              :id => "country",
              :name => "country"

    .clear


    = f.button :submit, t(:submit, :scope => :register) + " »"
  accepts_nested_attributes_for :profile
  validates_presence_of :gender
  validates_presence_of :city
  validates_presence_of :country
-- !ruby/hash:ActiveSupport::HashWithIndifferentAccess
utf8: ✓
authenticity_token: <mytokenhere>
user: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
  invite_code: ''
  username: username
  email: ''
  password: ''
  password_confirmation: ''
  profile_attributes: !ruby/hash:ActiveSupport::HashWithIndifferentAccess
    form: signup
    gender: male
    dob(3i): '9'
    dob(2i): '7'
    dob(1i): '1942'
    city: 'somevalue'
    country: 'somevalue'
commit: Register »
action: create
controller: registrations
- resource.build_profile
- resource.build_profile unless resource.profile

这样做的目的不是清理配置文件,而是使用旧配置文件(如果可用)。。。所以不要清除所有的错误!希望这对将来的人有帮助

尝试更改为
验证在:%w(男-女)
中包含的性别,并查看是否会给您一条错误消息thx尝试了但没有成功。我尝试了一些其他配置文件字段,我将添加到表单中,也许这是一个与性别相关的问题。您是否尝试将
验证\u关联的:配置文件
放在您的
用户
模型中?@HargrimmTheBleak是的,刚刚尝试了相同的结果,重新启动了应用程序,但都无法工作。此外,我在配置文件表中的性别旁边放置了一个位置,并设置了validations OK。也许您可以将控制器操作粘贴到这里?发布表单时,您在
params
中得到了什么?我对此进行了检查,但事实并非如此,性别不是set@Rubytastic:您已经给了
用户新的
对象<代码>性别
在配置文件表中,对吗?你能检查一下Profile.new的输出吗?我更新了Profile.new的输出。今晚详细查看本主题中提供的所有答案和解决方案,thx支持。如果我将3个字段留空,表单仍将验证,没有错误(我将3个配置文件字段设置为必需)。谢谢你的评论,并花了一些时间来写一个例子。我明天会详细检查他们,然后再报告。再次使用thx。我使用了您示例中的“循环所有错误”代码,一点也不想尝试错误样式设置。因此字段无效,但没有CSS,表单也不会重新填充。我现在要剥离我的配置文件模型,看看是否有代码导致了这些问题。是的,Rails 2有一个
方法,但它在Rails 3中被删除了,因为核心团队觉得它太“神奇”,因为没有人真正理解它做了什么。所以,现在你必须自己做造型,但不管怎么说,这并不是更多的工作。表单可能无法重新填充,因为您的
资源。生成配置文件
,它可能会用全新(空白)配置文件覆盖以前的(无效)配置文件。是否存在