# Reusing partials

**URL:** <https://www.ruby-forum.com/t/reusing-partials/110249>\
**Category:** Rails\
**Created:** [July 24, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/reusing-partials/110249 "2007-07-24T17:04:51Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@matt\_99](https://www.ruby-forum.com/u/matt_99)\
**Post date:** [July 24, 2007, 5:04pm UTC](https://www.ruby-forum.com/t/reusing-partials/110249/1 "2007-07-24T17:04:51Z")

</div>

Might be asking a daft question or missing something obvious here as  
I’m a bit of noob at this, I’ve got the code below in a partial and am  
using it to display and attach skills to degrees which is working  
fine, I now need to do exactly the same thing for interests and was  
wondering if there is any way to pass the partial a parameter which I  
can then some how be used to replace the word skill in the code and  
reuse the partial for both, is this doable or do I just need to copy  
the code into another file and have a separate partial for interests

\<%= form\_tag :action =\> “add\_some\_skills”,  
:id=\> @degree %\>

\<% @unused\_skills.each {|key| %\>  
\<%= check\_box(‘key’+key.id.to\_s, ‘checked’) +  
key.name %\>  
  
\<% }%\>  
\<%= submit\_tag %\>  
\<%= end\_form\_tag %\>

\<% if @degree.skills.empty?%\> (None)

\<% else %\> \<% for skill in @degree.skills %\> \<%= skill.name %\>&nbsp; \<%= link\_to 'Remove', {:action =\> 'remove\_skill', :id=\> @degree, :which\_skill =\> skill.id}, :confirm =\> 'Are you sure?', :post =\> true %\>  
\<% end %\> \<% end %\>

---

<div class="post-metadata">

**Author:** ![matt\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@matt\_99](https://www.ruby-forum.com/u/matt_99)\
**Post date:** [July 24, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/reusing-partials/110249/2 "2007-07-24T18:28:24Z")

</div>

Matt,

Here’s my take on your partial. Hope you don’t mind that I cleaned up  
your code formatting and style a little bit. (Made it easier to  
understand what was going on.)

Your main question was how to make this partial reusable for both  
skills and interests. The answer is to use the :locals hash option of  
the render :partial method. You would now call this partial with code  
that looks as follows:

render :partial =\> ‘degree\_items’, :locals =\> { :degree =\> @degree,  
:items =\> @degree.skills, :unused\_items =\> @unused\_skills,  
:add\_action =\> “add\_some\_skills”, :remove\_action =\> “remove\_skill” }

The key/value pairs in the :locals hash become local variables in the  
partial. The parameterization of the significant variables should  
allow you to call it in a similar way for interests.

 \<% form\_tag(:action =\> add\_action, :id =\> degree) do %\> \<% unused\_items.each do |key| %\> \<%= check\_box('key'+key.id.to\_s, 'checked') + key.name %\>  
 \<% end %\> \<%= submit\_tag %\> \<% end %\> \<% content\_tag(:p, "(None)") if items.empty? %\> \<% for item in items %\> \<%= item.name %\>&nbsp; \<%= link\_to 'Remove', { :action =\> remove\_action, :id =\> degree, :which\_skill =\> item.id}, :confirm =\> 'Are you sure?', :post =\> true %\>  
 \<% end %\>

Other little things:

- calling form\_tag with a block parameter is much preferred nowadays
- I tightened up your if/else code for the items list. (Since the for  
loop won’t fire for an empty collection anyway.)

HIH,  
Obie

On 7/24/07, Matt\_99 [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> \<% end %\> \<% end %\>
> 
> >

–  
Obie F.

> **[Best Online Casino in Australia | Top Licensed Casinos for Gamblers](https://toponlinecasinoaustralia.com)**
>
> Best Online Casino in Australia with licensed games only. Play blackjack, roulette, poker and other top-notch casino games. Enjoy gambling 24/7.

Pre-order my book The Rails Way today!  
[http://www.amazon.com/dp/0321445619](http://www.amazon.com/dp/0321445619)

---

<div class="post-metadata">

**Author:** ![matt\_99](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/e19adc/32.png) [@matt\_99](https://www.ruby-forum.com/u/matt_99)\
**Post date:** [July 24, 2007, 6:28pm UTC](https://www.ruby-forum.com/t/reusing-partials/110249/3 "2007-07-24T18:28:56Z")

</div>

Matt\_99 wrote:

> Might be asking a daft question or missing something obvious here as  
> I’m a bit of noob at this, I’ve got the code below in a partial and am  
> using it to display and attach skills to degrees which is working  
> fine, I now need to do exactly the same thing for interests and was  
> wondering if there is any way to pass the partial a parameter which I  
> can then some how be used to replace the word skill in the code and  
> reuse the partial for both, is this doable or do I just need to copy  
> the code into another file and have a separate partial for interests
> 
> \<%= form\_tag :action =\> “add\_some\_skills”,  
> :id=\> @degree %\>
> 
> \<% @unused\_skills.each {|key| %\>  
> \<%= check\_box(‘key’+key.id.to\_s, ‘checked’) +  
> key.name %\>  
>   
> \<% }%\>  
> \<%= submit\_tag %\>  
> \<%= end\_form\_tag %\>
> 
> \<% if @degree.skills.empty?%\> (None)
> 
> \<% else %\> \<% for skill in @degree.skills %\> \<%= skill.name %\>&nbsp; \<%= link\_to 'Remove', {:action =\> 'remove\_skill', :id=\> @degree, :which\_skill =\> skill.id}, :confirm =\> 'Are you sure?', :post =\> true %\>  
> \<% end %\> \<% end %\>
> 
> Although I have never done this I would offer the following replies.
> 
> 1. NO. Since a Skill has nothing to do with an Interest, leave them  
> separate in case their functionality diverges in the future.
> 
> OR
> 
> 1. Look up Ruby Meta programming. This is using Ruby code to write  
> dynamic code. It’s a bit heavier on the technical side but seems to be a  
> real asset in your toolbox if you pick it up. (It’s on my to-learn list  
> after I complete the current site I’m working on.)
> 
> P.S. Rails is almost all Meta programming under the hood.
> 
> OR
> 
> 1. Seems to me you’re just creating lists here. Why not use a generic  
> “create list” and have the users categorize it (from a per determined  
> list of categories). Then you can just run a loop that will a. display  
> skills category and b. display the interests category.
> 
> Hope this helps.
