# Sharing a partial between multiple controllers

**URL:** <https://discuss.rubyonrails.org/t/sharing-a-partial-between-multiple-controllers/36933>\
**Category:** rubyonrails-talk\
**Created:** [December 2, 2008, 5:24pm UTC](https://discuss.rubyonrails.org/t/sharing-a-partial-between-multiple-controllers/36933 "2008-12-02T17:24:47Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [December 2, 2008, 5:24pm UTC](https://discuss.rubyonrails.org/t/sharing-a-partial-between-multiple-controllers/36933/1 "2008-12-02T17: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:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [December 2, 2008, 5:27pm UTC](https://discuss.rubyonrails.org/t/sharing-a-partial-between-multiple-controllers/36933/2 "2008-12-02T17:27:14Z")

</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\>"

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

Fred

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [December 2, 2008, 6:40pm UTC](https://discuss.rubyonrails.org/t/sharing-a-partial-between-multiple-controllers/36933/3 "2008-12-02T18:40:16Z")

</div>

> Frederick Cheung 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 was actually thinking of something along the lines for \<% fields\_for @payee do |f| %\> &nbsp;&nbsp;&nbsp;\<%= render :partial=\> 'shared/location', :object =\> f %\> \<% end \>%

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [December 2, 2008, 6:49pm UTC](https://discuss.rubyonrails.org/t/sharing-a-partial-between-multiple-controllers/36933/4 "2008-12-02T18:49:18Z")

</div>

Frederick Cheung wrote:

> I was actually thinking of something along the lines for \<% fields\_for @payee do |f| %\> &nbsp;&nbsp;&nbsp;\<%= 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
