【问题标题】:Rails 4: two models with polymorphic associations (has one and belongs_to) and nested_attributesRails 4:两个具有多态关联的模型(有一个和belongs_to)和nested_attributes
【发布时间】:2014-06-04 18:04:24
【问题描述】:

我有两个模型:

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

我的注册控制器

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 对象即使有效也不会被保存。但是用户对象被保存(虽然当人员无效时不应该)。

为什么它不起作用?

【问题讨论】:

    标签: ruby-on-rails-4 nested-attributes polymorphic-associations


    【解决方案1】:

    我的许可参数不正确:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多