2012-07-26 28 views
1

這是在導軌2.3上。我削減了一堆代碼,希望能夠解決問題。當我點擊1個或多個EmailPreferences用戶對象上保存,我得到「不包含在列表中」when fields_for on one-to-many

1錯誤,無法儲存禁止該用戶

有與以下領域的問題:

通知類型是不包含在列表

class User < ActiveRecord::Base 
    has_many :email_preferences 
    accepts_nested_attributes_for :email_preferences 
    attr_accessible :email_preferences_attributes 
end 

class EmailPreference < ActiveRecord::Base 
    # receives is a boolean, notification_type is a string. 
    attr_accessible :user_id, :receives, :notification_type 
    belongs_to :user 
end 

class UsersController < ApplicationController 
    def new 
    @user = User.new 
    @user.email_preferences.build :receives=>false, :notification_type=>"outage" 
    end 

    def edit 
    @user = User.find(params[:id]) 
    end 
end 

# app/views/user/_form.rhtml 
<% form_for :user, user do |f| -%> 
    <% f.fields_for :email_preferences do |preference_form| -%> 
    <dd> 
     <%= preference_form.check_box :receives %> 
     <!-- I just want to display the notification type. I do not want to edit it. --> 
     <%= preference_form.object.notification_type %> 
    </dd> 
    <% end -%> 
    <%= form_submit f.object, :cancel => companies_path %> 
<% end -%> 

編輯

更妙的是,竟被如何d您可以瞭解調試不太有用的錯誤消息嗎?

回答

0

好吧,添加一個隱藏的輸入似乎工作。它也看起來像Rails不需要隱藏的notification_type,如果電子郵件首選項已經在數據庫中。所以也許規則是對於新的子對象,你需要包含所有必需的字段。

<% f.fields_for :email_preferences do |preference_form| -%> 
    <dd> 
    <%= preference_form.check_box :receives %> 
    <!-- I just want to display the notification type. I do not want to edit it. --> 
    <%= preference_form.object.name %> 
    <%= preference_form.hidden_field :notification_type %> 
    </dd> 
<% end -%>