1

I have a page where I list out a bunch of loans that a user has and I want a form tag around each one of those forms because they can make a payment on any of the loans that are listed.

Here's what I have that is throwing the error "Undefined method 'loan'..."

<% @loans.each do |loan| %>

    <% form_for loan add_payment_payment_path(loan) , :method => :post do |f| %>                                       
        <%= f.text_field :amount, :placeholder => 'Amount' %>
        <%= f.submit 'Make Payment', :class => 'btn btn-primary'%>
    <% end %>

<% end %>

How can i have a dynamic form_for inside my @loans.each loop?

2 Answers 2

4

You're form_for call is incorrect, it should be:

<% form_for loan, url: add_payment_payment_path(loan), method: :post do |f| %>

You're missing the comma after loan, so it's getting interpreted as a method with add_payment_payment_path(loan) as its argument.

Also note that passing a url to form_for is done via the options hash, using the :url key. It is not a separate parameter, and passing it as such will get you a wrong number of arguments (3 for 2) error.

Sign up to request clarification or add additional context in comments.

3 Comments

Ok the page loads now, but the form does not render.
What do you mean by doesn't render? Doesn't appear on the page at all? Have you inspected the HTML to see what is being output? This sounds somewhat odd, and is quite possibly a separate issue.
I believe it is a separate issue after looking at it again.
1

You have two syntax errors in your form_for method. Your missing a comma after loan and also you have an extra space after add_payment_payment_path(loan)

<% form_for loan add_payment_payment_path(loan) , :method => :post do |f| %>

Change this to

<% form_for loan, add_payment_payment_path(loan), :method => :post do |f| %>

Remember in Ruby parenthesis are optional in most cases, however, you must keep only one space. Here is the same form_for method with the parenthesis

<% form_for(loan, add_payment_payment_path(loan), :method => :post) do |f| %>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.