1

在我的 rails 应用程序中,我正在以嵌套形式创建一个对象“组织”和两个子对象“用户”和“设置”。当我运行表单时,我收到错误:

WARNING: Can't mass-assign protected attributes: user

我以前在另一个项目中遇到过这个问题,发现我需要

@organization.users.build

在 organizations_controller/new 中。为了在提交表单时添加子对象并创建一个子对象。

organization.rb
class Organization < ActiveRecord::Base
  attr_accessible :org_name, :address1, :address2, :city, :state, :postal_code, :country, :logo_image, :setting_id, :active, :status_image, :users_attributes, :setting_attributes
  has_many :users, :dependent => :destroy
  has_one :setting, :dependent => :destroy
  accepts_nested_attributes_for :users
  accepts_nested_attributes_for :setting
end

user.rb
class User < ActiveRecord::Base
  attr_accessible :email, :password, :password_confirmation, :name_first, :name_last, :admin, :notes, :current_outstanding, :pin, :registered, :sms, :super_user, :organization_id

  belongs_to :organization
end

setting.rb
class Setting < ActiveRecord::Base
  attr_accessible :green_hours, :green_mins, :yellow_hours, :yellow_mins, :red_hours,   :red_mins, :reminder_sms, :reminder_sms_hours, :reminder_sms_mins, :logo_url, :comp_name, :alert_emails, :alert_smss, :history_hours, :time_zone, :user_id, :organization_id

  belongs_to :organization
end

organizations_controller.rb
class OrganizationsController < ApplicationController

  def new
    @organization = Organization.new
    @organization.build_setting
    @organization.users.build

    respond_to do |format|
      format.html { render :layout => 'application_dashboard_orgs' }
      format.json { render json: @organization }
    end
  end

  def create
    @organization = Organization.new(params[:organization])

    respond_to do |format|
      if @organization.save
        format.html { redirect_to organizations_path, notice: 'Organization was successfully created.' }
        format.json { render json: @organization, status: :created, location: @organization }
      else
        format.html { render action: "new" }
        format.json { render json: @organization.errors, status: :unprocessable_entity }
      end
    end
  end
end

organizations/_form.html.erb
<%= form_for(@organization) do |f| %>
  <div class="Col1">

  <div class="MenuItem"><div class="MenuItemLabel">Account Name:</div> <%= f.text_field :org_name %></div>
  <div class="MenuItem"><div class="MenuItemLabel">Address Line 1:</div> <%= f.text_field :address1 %></div>
  <div class="MenuItem"><div class="MenuItemLabel">Address Line 2:</div> <%= f.text_field :address2 %></div>
  <div class="MenuItem"><div class="MenuItemLabel">City:</div> <%= f.text_field :city %></div>
  <div class="MenuItem"><div class="MenuItemLabel">State:</div> <%= f.text_field :state %></div>
  <div class="MenuItem"><div class="MenuItemLabel">Postal Code:</div> <%= f.text_field :postal_code %></div>
  <div class="MenuItem"><div class="MenuItemLabel">Country:</div> <%= f.text_field :country %></div>
  <div class="MenuItem"><div class="MenuItemLabel">Logo Image:</div> <%= f.file_field :logo_image %></div>
  <div class="MenuItem"><div class="MenuItemLabel">Active Account?</div> <%= f.check_box :active %></div>
  </div>

<div class="Col2">
  <div class="MenuItem"><div class="MenuItemLabel"><b>Administrator Account</b></div></div>
  <%= f.fields_for :user do |user| %>
      <div class="MenuItem"><div class="MenuItemLabel">First Name:</div> <%= user.text_field :name_first %></div>
      <div class="MenuItem"><div class="MenuItemLabel">Last Name:</div> <%= user.text_field :name_last  %></div>
      <div class="MenuItem"><div class="MenuItemLabel">Email Address:</div><%= user.text_field :email  %></div>
      <div class="MenuItem"><div class="MenuItemLabel">PIN:</div><%= user.password_field :password, :id => "password_field", :onchange => "add_pin()" %></div>
      <div class="MenuItem"><div class="MenuItemLabel">PIN Confirmation:</div><%= user.password_field :password_confirmation  %>  </div>
      <div class="MenuItem"><div class="MenuItemLabel">SMS Reminder Phone #:</div><%= user.text_field :sms  %>  </div>

      <div class="MenuItem"><div class="MenuItemLabel">Is Administrator:</div> <%= user.check_box :admin, {checked: true} %></div>
      <div class="MenuItem"><div class="MenuItemLabel">Password Setup?</div> <%= user.check_box :registered   %></div>
  <% end %>
  <%= f.fields_for :setting do |setting| %>
      <%= setting.hidden_field :green_hours, :value => params[99] %>
      <%= setting.hidden_field :green_mins, :value => params[59] %>
      <%= setting.hidden_field :yellow_hours, :value => params[0] %>
      <%= setting.hidden_field :yellow_mins, :value => params[15] %>
      <%= setting.hidden_field :red_hours, :value => params[0] %>
      <%= setting.hidden_field :red_mins, :value => params[0] %>
      <%= setting.hidden_field :reminder_sms, :value => params[true] %>
      <%= setting.hidden_field :reminder_sms_hours, :value => params[0] %>
      <%= setting.hidden_field :reminder_sms_mins, :value => params[15] %>
      <%= setting.hidden_field :history_hours, :value => params[24] %>
  <% end %>
      <div class="MenuItem">
    <%= flash[:notice] %> <br />
    <%= f.submit "Save Settings" %> <!--<input type="reset" value="Reset" />  -->

  </div>

  <div class="clr"></div>

</div>
4

3 回答 3

3
<%= f.fields_for :user do |user| %>

应该

<%= f.fields_for :users do |user| %>
于 2012-05-30T20:49:38.483 回答
2

我认为您想要做的是使用表单支持的对象。Jeff Dean 在http://pivotallabs.com/users/jdean/blog/articles/1706-form-b​​acking-objects-for-fun-and-profit上有一篇很好的博客文章

于 2012-05-30T19:44:34.077 回答
0

您的错误表明您在尝试保存组织时无法设置“用户”属性。尝试将:userand添加:setting到您的 attr_accessible 行organization.rb

于 2012-05-30T19:54:08.437 回答