【问题标题】:Form with nested models with many to many through relationship "can't assign mass attributes" error具有多对多通过关系的嵌套模型形成“无法分配质量属性”错误
【发布时间】:2014-04-24 21:19:49
【问题描述】:

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

此行出现错误

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

控制器

family_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

【问题讨论】:

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


    【解决方案1】:

    FamilyUser 关联一维关系。

    在你看来families/new.html.erb

    改变

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

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

    【讨论】:

    • 是的,我很抱歉。我忘记了我所在的代码库,由于其他依赖关系,我正在使用 Rails 3:/ 我已经更新了问题。
    • @MicFin 没问题。但我确实浪费了相当多的时间。请参阅我的更新答案。
    • 非常感谢您的帮助。我进行了建议的更改,但仍然收到错误消息。我相信这是因为我的 users_attributes 出于某种原因创建了一个“0”哈希。有什么建议? => {"utf8"=>"✓", "authenticity_token"=>"EqWGxK3Fuj2uYk3namWK9SbXLPRKSn6cReT7wQddG0E=", "family"=> {"name"=>"test Family", "users_attributes"=>{"0"=>{ "first_name"=>"jane", "last_name"=>"smith"}}}, "commit"=>"Create Family", "action"=>"create", "controller"=>"family"}
    • 您是否得到完全正确的Can't mass-assign protected attributes: user" 错误?或者是其他东西?注意错误中指定的user。由于上述更改应该已经解决了。
    • 至于“0”部分,因为您将为family 存储多个users 记录,它从第0 个元素开始索引。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-01-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    相关资源
    最近更新 更多