New to rails and trying to get a one to many relationship up and running. I have it working in the Model and Controller, but I'm having trouble getting it up and running in the views.
class Project < ActiveRecord::Base
has_many :non_labor_expenses
end
class NonLaborExpense < ActiveRecord::Base
belongs_to :project
end
I manually created some entries in the the non_labor_expense table by loading the @non_labor_expenses in the controller (edit action) and can pull the existing data in the project view like so:
<% 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 %>
<% end %>
What I am having trouble doing is adding a new non_labor_expense entry to the project. I should be able to manage handling it on the backend, but I can't even get the field to show up in the form.
Here's where I'm at now:
<li class="editable">
<div class="label"><%= f.label :non_labor_expenses %></div>
<%= f.text_field :non_labor_expenses %>
</li>
I know my above code looks nothing like this, but ideally the form fields would be something like:
Expense Amount [text input]
Expense Description [text input]
My full form code can be found here: http://pastebin.com/m2b280b0f