1

私の Rails アプリでは、オブジェクト「Organization」と 2 つのサブオブジェクト「User」と「Settings」をネストされた形式で作成しています。フォームを実行すると、次のエラーが表示されます。

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

私は以前に別のプロジェクトでこの問題を抱えていましたが、

@organization.users.build

organization_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 に答える