Reusing partials

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 %>  <%= link_to 'Remove', {:action => 'remove_skill', :id=> @degree, :which_skill => skill.id}, :confirm => 'Are you sure?', :post => true %>
<% end %> <% end %>

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 %>  <%= 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 [email protected] wrote:


<% end %> <% end %>


Obie F.

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

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 %>  <%= 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.