John Cowan
John Cowan

Reputation: 1674

Rails Passing Object to Form Partial

rails 5, ruby 2.5

I'm trying something new for me. In my head it makes sense but it is not working. I want to put a <form> tag on my new and edit views, then call a partial, which has the rest of the form. I'm passing in the "instance variable" (correct term?) f using :locals, but it is not recognized in the partial.

# new.html.erb

<% @page_title = "New Food Entry" %>

<%= form_for(@food) do |f| %>

  <%= render "form", :locals => { :f => f  } %> 

<% end %>
# _form.html.erb

<table cellpadding="2" cellspacing="0" style="border: 1px solid #369;" summary="Edit or new Food Record">

  <tr>                                                                        
    <td><label>Food Name</label></td>                                                   
    <td><%= f.text_field :food_item, :size => 30 %></td>
  </tr>
...

When I load .../foods/new, I get this error:

undefined local variable or method `f' for #<#<Class:0x00007fdb4c036428>:0x00007fdb342a3890

Any tips would be much appreciated.

Upvotes: 0

Views: 831

Answers (1)

Andrew Zelenets
Andrew Zelenets

Reputation: 113

According to the Rails docs. Please use <%= render partial: "form", :locals => { :f => f } %> or <%= render "form", :f => f %> syntax.

Upvotes: 1

Related Questions