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
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:
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.
<td>
<% form_tag(:action => add_action, :id => degree) do %>
<% unused_items.each do |key| %>
<%= check_box('key'+key.id.to_s, 'checked') + key.name %><br/>
<% end %>
<%= submit_tag %>
<% end %>
</td>
<td>
<% content_tag(:p, "(None)") if items.empty? %>
<% for item in items %>
<%= item.name %>
<%= link_to 'Remove', { :action => remove_action, :id => degree,
:which_skill => item.id},
:confirm => 'Are you sure?', :post => true %><br/>
<% end %>
</td>
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.)