Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to render a form partial in Rails 4?

undefined local variable or method `f' for #<#:0x000001080edfe0>

I am trying to render a form within a template page with:

<%= form_for @vehicle, html: { class: "f_grp" }, remote: true do |f| %>
  <%= render "vehicles", locals: { f: f } %>
<% end %>

The file is loading. But I'm getting an undefined method on f error. Any ideas?

like image 768
MrPizzaFace Avatar asked Feb 26 '14 05:02

MrPizzaFace


1 Answers

You only use :locals when you use :partial.

Either of these are correct:

<%= render partial: 'vehicles', locals: { f: f } %>

Or (as of Rails 2.3):

<%= render 'vehicles', f: f %>

Your version, which combines both, creates a local called locals and sets its value to a hash containing f: FormBuilder....

It's worth noting that the only reason to still use locals: is when you use render :collection.

like image 114
meagar Avatar answered Oct 16 '22 08:10

meagar