# Sharing a partial between multiple controllers

**URL:** <https://www.ruby-forum.com/t/sharing-a-partial-between-multiple-controllers/154659>\
**Category:** Rails\
**Created:** [December 2, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/sharing-a-partial-between-multiple-controllers/154659 "2008-12-02T18:24:47Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [December 2, 2008, 6:24pm UTC](https://www.ruby-forum.com/t/sharing-a-partial-between-multiple-controllers/154659/1 "2008-12-02T18:24:47Z")

</div>

I want to share a partial between 2 or more controllers.

I have a “location” partial that I want users to enter “Address, City,  
State, Zip” information in however I have “Payees”, “Institution”, and  
“Event” models that can have “location” information stored with them.  
Since I don’t want to duplicate the partial across each controller, I’ve  
placed the partial in a “shared” folder however I’m having problems  
rendering it with the appropriate model object.

A sample of my renders look like this:  
\<%= render :partial =\> ‘shared/location’, :object =\> @payee %\>

A sample of my partial looks like this:  
\<%= location.text\_field :address, :size =\> 60 %\>

Obviously rails is complaining that the object is not related to a form  
since I’m not using “form\_for” or “fields\_for” (see below for error).

“undefined method `text\_field’ for #Payee:0x4472530”

How can I make this happen while keeping it DRY?

---

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [December 2, 2008, 6:28pm UTC](https://www.ruby-forum.com/t/sharing-a-partial-between-multiple-controllers/154659/2 "2008-12-02T18:28:03Z")

</div>

On 2 Dec 2008, at 17:24, Corey M. wrote:

> “undefined method `text\_field’ for #Payee:0x4472530”

Could you just call fields\_for and pass through the yielded object  
instead of passing through @payee ?

Fred

---

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [December 2, 2008, 7:35pm UTC](https://www.ruby-forum.com/t/sharing-a-partial-between-multiple-controllers/154659/3 "2008-12-02T19:35:22Z")

</div>

Frederick C. wrote:

> On 2 Dec 2008, at 17:24, Corey M. wrote:
> 
> > “undefined method `text\_field’ for #Payee:0x4472530”
> 
> Could you just call fields\_for and pass through the yielded object  
> instead of passing through @payee ?
> 
> Fred

How would I pass through the yielded object? I can’t write “fields\_for  
:payee” since this partial is used by the other two models (events,  
institutions) as well.

When trying the following . . .

\<%= render :partial =\> ‘shared/location’, :object =\> @payee %\>

\<% fields\_for :location do |l| %\>  
\<%= l.text\_field :address, :size =\> 60 %\>  
\<% end %\>

The rendered HTML is showing the field ids as such “location\_address”,  
when they need to ultimately be “payee\_address”

I’m obviously missing something here.

---

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [December 2, 2008, 7:49pm UTC](https://www.ruby-forum.com/t/sharing-a-partial-between-multiple-controllers/154659/4 "2008-12-02T19:49:18Z")

</div>

Frederick C. wrote:

> I was actually thinking of something along the lines for  
> \<% fields\_for @payee do |f| %\>  
> \<%= render :partial=\> ‘shared/location’, :object =\> f %\>  
> \<% end \>%

I follow you now. A much simpler way of doing it and it solved my  
problem.

Thanks once again Fred

---

<div class="post-metadata">

**Author:** ![rubylicious](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/91b2a8/32.png) [@rubylicious](https://www.ruby-forum.com/u/rubylicious)\
**Post date:** [December 2, 2008, 7:41pm UTC](https://www.ruby-forum.com/t/sharing-a-partial-between-multiple-controllers/154659/5 "2008-12-02T19:41:31Z")

</div>

On 2 Dec 2008, at 18:35, Corey M. wrote:

> > Fred  
> > \<% fields\_for :location do |l| %\>  
> > \<%= l.text\_field :address, :size =\> 60 %\>  
> > \<% end %\>
> 
> The rendered HTML is showing the field ids as such “location\_address”,  
> when they need to ultimately be “payee\_address”

I was actually thinking of something along the lines for  
\<% fields\_for @payee do |f| %\>  
\<%= render :partial=\> ‘shared/location’, :object =\> f %\>  
\<% end \>%
