1

Rails 3 で複数のネストされたフォームを作成しています。formtastic_cocoon ジェムを使用していますが、この問題とはあまり関係がないと思います。

私にはユーザーがいて、ユーザーにはタスクがあり、タスクにはステップがあります。ネストは、users>tasks>steps です。ユーザーに対してタスク フィールドを動的に追加および削除し、タスクからステップ フィールドを動的に追加および削除できます。

ただし、フォームを送信すると、ユーザーはタスクを取得しますが、タスク>ステップはデータベースに保存されません。

Rails はエラーを返さず、何も起こりません。

私のモデルは

クラス User < ActiveRecord::Base
    act_as_authentic

    has_many :タスク
        accept_nested_attributes_for :tasks, :reject_if=> proc {|attributes| attributes[:entry].blank?}, :allow_destroy => true

終わり

クラス タスク < ActiveRecord::Base
          attr_accessible :エントリ

          所属先:ユーザー
          has_many :steps
         accept_nested_attributes_for :steps, :reject_if=> proc {|attributes| attributes[:title].blank?}, :allow_destroy => true
終わり

クラス Step < ActiveRecord::Base
        attr_accessible :タイトル

        所属先:タスク
終わり

私の form.html.erb には

<%= @ユーザーのセマンティックフォーム %>
    <%= form.inputs :ユーザー名, :パスワード %>
    <div>
      <% form.semantic_form_fields_for :tasks do |builder| %>
         <%= render 'task_fields', :f=>builder %>
      <%終了%>
   <%= link_to_add_association 'タスクの追加', フォーム, :タスク %>
   </div>

_task_fields.html.erb は次のようになります

<div class="nested-fields">
     <%= link_to_remove_association "タスクの削除", f %>
        <%= f.inputs :entry %>
          <div>
             <% f.semantic_form_fields_form :steps do |builder| %>
              <%= レンダリング 'step_fields' :f => ビルダー %>
             <%終了%>
           <%= link_to_add_association 'ステップを追加', f, :steps %>
          </div>
</div>

最後に、_step_fields.html.erb ページは

  <div class="nested-fields">
   <%= link_to_remove_association "削除ステップ", f %>
    <%= f.inputs :タイトル %>
  </div>
4

1 に答える 1

1

ログにこれが表示されますか?:

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

その場合は、これを Task モデルに追加します。

attr_accessible :steps_attributes
于 2011-02-10T02:27:45.427 に答える