Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.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 4 Rails 4:两个具有多态关联(有一个且属于)和嵌套的属性的模型_Ruby On Rails 4_Nested Attributes_Polymorphic Associations - Fatal编程技术网

Ruby on rails 4 Rails 4:两个具有多态关联(有一个且属于)和嵌套的属性的模型

Ruby on rails 4 Rails 4:两个具有多态关联(有一个且属于)和嵌套的属性的模型,ruby-on-rails-4,nested-attributes,polymorphic-associations,Ruby On Rails 4,Nested Attributes,Polymorphic Associations,我有两种型号: # == Schema Information # # Table name: users # # id :integer not null, primary key # email :string(255) # password_digest :string(255) # created_user :integer # updated_user :integer # created_at

我有两种型号:

# == Schema Information
#
# Table name: users
#
#  id              :integer          not null, primary key
#  email           :string(255)
#  password_digest :string(255)
#  created_user    :integer
#  updated_user    :integer
#  created_at      :datetime
#  updated_at      :datetime
#  remember_token  :string(255)
#
class User < ActiveRecord::Base
  has_one :person, as: :personable
  accepts_nested_attributes_for :person, allow_destroy: true
  validates_associated :person
end
#==架构信息
#
#表名:用户
#
#id:整数不为空,主键
#电子邮件:string(255)
#密码摘要:字符串(255)
#已创建用户:整数
#更新用户:整数
#创建时间:datetime
#更新时间:datetime
#记住\u标记:字符串(255)
#
类用户

#==架构信息
#
#表名:人
#
#id:整数不为空,主键
#姓氏:字符串(255)
#名字:字符串(255)
#中间名称:字符串(255)
#生日:日期
#已创建用户:整数
#更新用户:整数
#人物id:integer
#人物类型:字符串(255)
#创建时间:datetime
#更新时间:datetime
#
类Person
我的注册控制器

class SignupController < ApplicationController

  layout "signup"

  def new
    @user = User.new
    @user.build_person
  end

  def create
    @user = User.new(user_params)
    if @user.save
      save_user_id
      redirect_to @user
    else
      render "signup/new"
    end
  end

  private

  def user_params
    params.require(:user).permit(:email,
                                 :password,
                                 :password_confirmation,
                                 people_attributes: [ :last_name,
                                                      :first_name,
                                                      :birthday])
  end
end
类注册控制器
我的视图注册/new.html.erb

<%= form_for(@user, url: signup_path, html: { role: "form" }) do |user_fields| %>
  <%= user_fields.fields_for :person do |person_fields| %>
    <%= person_fields.text_field :last_name %>
    <% if @user.errors.any? %>
      <ul class="text-danger">
      <% @user.errors.each do |msg| %>
        <li><%= msg %></li>
      <% end %>
      </ul>
    <% end %>
  <% end %><!-- /fields_for person -->

  ...

<% end %><!-- /form -->

...
当我以ModelPersons的形式创建一个新对象时,即使它无效,也不会返回error的形式。并且即使person对象有效,也不会保存它。但用户对象已保存(但在person无效时不应保存)


为什么不工作?

我的许可参数不正确:

params.require(:user).permit(:email,
                             :password,
                             :password_confirmation,
                             person_attributes: [ :last_name,
                                                  :first_name,
                                                  :birthday])
反而

params.require(:user).permit(:email,
                             :password,
                             :password_confirmation,
                             people_attributes: [ :last_name,
                                                  :first_name,
                                                  :birthday])
params.require(:user).permit(:email,
                             :password,
                             :password_confirmation,
                             people_attributes: [ :last_name,
                                                  :first_name,
                                                  :birthday])