1

これはレール2.3にあります。うまくいけば問題のポイントに到達するために、私はたくさんのコードを切り取りました。1つ以上の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 -%>

編集

さらに良いことに、あまり役に立たないエラーメッセージをデバッグするにはどうすればよいでしょうか。

4

1 に答える 1

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 -%>
于 2012-07-26T20:45:49.720 に答える