レールに不慣れで、1対多の関係を稼働させようとしています。モデルとコントローラーで動作していますが、ビューで起動して実行するのに問題があります。
class Project < ActiveRecord::Base
has_many :non_labor_expenses
end
class NonLaborExpense < ActiveRecord::Base
belongs_to :project
end
コントローラに@non_labor_expensesをロードして(編集アクション)、non_labor_expenseテーブルにいくつかのエントリを手動で作成し、次のようにプロジェクトビューで既存のデータをプルできます。
<% unless @non_labor_expenses.nil? %>
<% count = 1 %>
<% for expense in @non_labor_expenses %>
<li>
<div class="label"><%= f.label :expense , "Expense" + count.to_s %></div>
<%= '$' + expense.amount.to_s + ' - ' + expense.description.to_s %>
</li>
<% count = count +1 %>
<% end %>
私が問題を抱えているのは、プロジェクトに新しいnon_labor_expenseエントリを追加することです。バックエンドでの処理を管理できるはずですが、フィールドをフォームに表示することすらできません。
これが私が今いるところです:
<li class="editable">
<div class="label"><%= f.label :non_labor_expenses %></div>
<%= f.text_field :non_labor_expenses %>
</li>
上記のコードはこのようには見えませんが、理想的にはフォームフィールドは次のようになります。
Expense Amount [text input]
Expense Description [text input]
私の完全なフォームコードはここにあります:http://pastebin.com/m2b280b0f