Rails Render part of a form inside another form

Viewed 6630

I have a page in which you create an invoice. There is a separate section that allows you to add payments to invoices. What I'm wanting to do is add the ability to create a payment when creating an invoice.

I'm wanting to render the "Create Payment" form VIEW into the "Create Invoice" form VIEW. How can I do this? Here is some code:

Invoice Form view (notice the render call):

<%= form_for(@contract) do |f| %>
  <div class="field">
    f.label "Add payment?"
    <div id="pay_form">
      <%= render 'payments/pay_form' %>
    </div>
</div>

(_pay_form.html.erb) The partial from the create payment form (notice I'm not including the form_for tag here because I don't want to next a form inside of another form on the Invoice page above):

<div class="field">
  <%= f.label 'Amount Paid:' %>
  <%= f.text_field :amount %>
</div>
<div class="field">
  <%= f.label 'Payment Method' %>
  <%= f.select :method, %w(Cash Credit Check) %>
</div>

The main problem is the f variable in the partial doesn't exist. And even if I assign the Invoice's f var from it's form, the names of the params would be params[:invoice][:amount] rather than params[:payment][:amount]. See what I'm saying?

What's the best way to go about doing this?

1 Answers
Related