形式与嵌套模型有很多对多关系"不能分配质量属性"错误

时间:2014-04-24 21:19:50

标签: forms ruby-on-rails-3.2 nested nested-forms nested-attributes

我坚持错误"无法批量分配受保护的属性:用户"为我的形式。我有一个用户通过单一表单创建一个家庭和其他用户。目前,我正在尝试创建一个功能,让用户在一个表单中创建一个新的系列和一个新用户。我安装了Cocoon gem并使用Rails 3.2.16。 https://github.com/nathanvda/cocoon

此行发生错误

families_controller.rb

  def create
    binding.pry
    @user = current_user
    @family = Family.new(params[:family]) <<<<<

参数是:

=> {"utf8"=>"✓",
 "authenticity_token"=>"EqWGxK3Fuj2uYk3namWK9SbXLPRKSn6cReT7wQddG0E=",
 "family"=>
  {"name"=>"test Family",
   "users_attributes"=>{"0"=>{"first_name"=>"jane", "last_name"=>"smith"}}},
 "commit"=>"Create Family",
 "action"=>"create",
 "controller"=>"families"}

模型

user.rb

class User < ActiveRecord::Base

  attr_accessible :first_name, :last_name, :age_months, :height_inches, :weight_ounces

  has_many :user_families
  has_many :families, through: :user_families
end

family.rb

class Family < ActiveRecord::Base
   attr_accessible :location, :name, :users_attributes, user_families_attributes

   has_many :user_families
   has_many :users, through: :user_families

   accepts_nested_attributes_for :users, :reject_if => :all_blank, :allow_destroy => true
   accepts_nested_attributes_for :user_families, :reject_if => :all_blank, :allow_destroy => true
end

查看

families.new.html.erb

<%= form_for(@family) do |f| %>

<form class = 'form-horizontal' role = 'form'>
  <div class='form-group'>
    <%= f.label :name %>
    <%= f.text_field :name, placeholder: 'Family Name' %>
  </div>
  <div class='form-group'>
  <%= f.fields_for @new_user do |ff| %>
    <%= label_tag :first_name %>
    <%= ff.text_field :first_name, placeholder: 'First Name' %>
    <%= label_tag :last_name %>
    <%= ff.text_field :last_name, placeholder: 'Last Name' %>
    <%= label_tag :age_months %>
    <%= ff.number_field :age_months, placeholder: 'Enter Age' %>
    <%= label_tag :height_inches %>
    <%= ff.number_field :height_inches, placeholder: 'Height in Inches' %>
    <%= label_tag :weight_ounces %>
    <%= ff.number_field :weight_ounces, placeholder: 'Weight in Pounds' %>
  <% end %>
  </div>
  <div class='actions'>
    <%= f.submit %>
  </div>
</form>
<% end %>

控制器

families_controller.rb

class FamiliesController < ApplicationController
  def index
    @families = Family.all
  end

  def show
    @user = current_user
    @family = Family.find(params[:id])
  end

  def new
    @user = current_user
    @family = Family.new(name: "#{@user.last_name} Family")
    @family.users.build
    @new_user = User.new
  end

  def edit
    @family = Family.find(params[:id])
  end

  def create
    @user = current_user
    @family = Family.new(params[:family])
    @family.users << @user
    @family.save
    redirect_to root_path
  end

  def update
    @family = Family.find(params[:id])
    @family.update_attributes(params[:family])
    @family.save
    redirect_to root_path(anchor: 'profile')
  end

  def destroy
    @family = Family.find(params[:id])
    @family.destroy
    redirect_to families_path
  end
end

1 个答案:

答案 0 :(得分:0)

FamilyUser与1-M关系相关联。

在您的观看中families/new.html.erb

更改

<%= f.fields_for @new_user do |ff| %>

<%= f.fields_for :users, @new_user do |ff| %>