# Using a partial inside a form\_for tag

**URL:** <https://www.ruby-forum.com/t/using-a-partial-inside-a-form-for-tag/128630>\
**Category:** Rails\
**Created:** [February 9, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/using-a-partial-inside-a-form-for-tag/128630 "2008-02-09T17:04:41Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [February 9, 2008, 5:04pm UTC](https://www.ruby-forum.com/t/using-a-partial-inside-a-form-for-tag/128630/1 "2008-02-09T17:04:41Z")

</div>

I have the following:

\<% form\_for @user do |f| -%\>  
…  
\<%= render :partial =\> “relationship\_status” %\>  
…  
\<% end -%\>

How do I pass f to the partial! 🙂 Or can I not do that?

–  
John K.  
removed\_email\_address@domain.invalid

Blog: [http://www.kopanas.com](http://www.kopanas.com)  
Conference: [http://www.cusec.net](http://www.cusec.net)  
Twits: [http://www.twitter.com/kopanas](http://www.twitter.com/kopanas)

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [February 9, 2008, 5:44pm UTC](https://www.ruby-forum.com/t/using-a-partial-inside-a-form-for-tag/128630/2 "2008-02-09T17:44:13Z")

</div>

On Feb 9, 8:03 am, “John K.” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have the following:
> 
> \<% form\_for @user do |f| -%\>  
> …  
> \<%= render :partial =\> “relationship\_status” %\>  
> …  
> \<% end -%\>
> 
> How do I pass f to the partial! 🙂 Or can I not do that?

Just pass the form to the partial as a ‘local’ like this:

\<%= render :partial =\> “relationship\_status” %, :locals =\> {:form =\>  
f } %\>

You can then refer to the form in your partial like this:

\<%= form.text\_field … %\>

---

<div class="post-metadata">

**Author:** ![johnk](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/59ef9b/32.png) [@johnk](https://www.ruby-forum.com/u/johnk)\
**Post date:** [February 10, 2008, 10:05pm UTC](https://www.ruby-forum.com/t/using-a-partial-inside-a-form-for-tag/128630/3 "2008-02-10T22:05:59Z")

</div>

Are you sure that you need to? If you are rendering relationship info  
for the user, but that relationship info relies on some other model,  
you might be able to:

\_relationship\_status.html.erb  
\<% fields\_for :relationships, relationships do |fld| %\>  
\<% fld.text\_field … %\>  
\<% end %\>

and pass the user into the partial:

\<%=  
render :partial=\>‘relationship\_status’,  
:locals=\>{:relationships=\>user.relationships}  
%\>

The main point being that if you’re rendering a partial that displays  
fields for a different model then you don’t need to pass the form  
variable, but a reference to that model or collection.
