这是在轨道2.3上。我把一堆代码剪掉了,希望能够解决问题。当我点击具有一个或多个EmailPreferences的User对象上的保存时,我得到了
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 -%>
修改的
更好的是,您将如何调试那些不太有用的错误消息?
答案 0 :(得分: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 -%>