Rails 4:两个具有多态关联(具有one和belongs_to)和nested_attributes的模型

时间:2014-06-04 18:04:24

标签: 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      :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

# == Schema Information
#
# Table name: people
#
#  id              :integer          not null, primary key
#  last_name       :string(255)
#  first_name      :string(255)
#  middle_name     :string(255)
#  birthday        :date
#  created_user    :integer
#  updated_user    :integer 
#  personable_id   :integer
#  personable_type :string(255)
#  created_at      :datetime
#  updated_at      :datetime
#

class Person < ActiveRecord::Base
  belongs_to :personable, polymorphic: true, dependent: :destroy
end

我的SignupController

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

当我以模型人的形式创建一个新对象时,即使它无效,也不会返回错误的形式。并且person对象即使有效也不会保存。但是保存了用户对象(尽管不应该在人员无效时保存)。

为什么它不起作用?

1 个答案:

答案 0 :(得分:0)

我的许可证参数不正确:

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])