Forum: Ruby on Rails reusing partials

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Matt_99 (Guest)
on 2007-07-24 19:04
(Received via mailing list)
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 %><br />
    <% }%>
<%= submit_tag %>
<%= end_form_tag %>

</td>
<td>
<p><% if @degree.skills.empty?%>
(None)
</p>
<% 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 %>
<br />
<% end %>
<% end %>
Obie F. (Guest)
on 2007-07-24 20:28
(Received via mailing list)
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.

<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 %>&nbsp;
    <%= 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.)

HIH,
Obie

On 7/24/07, Matt_99 <removed_email_address@domain.invalid> wrote:
>
> </td>
> <br />
> <% end %>
> <% end %>
>
>
> >
>
--
Obie F.
http://jroller.com/obie/

Pre-order my book The Rails Way today!
http://www.amazon.com/dp/0321445619
Jean N. (Guest)
on 2007-07-24 20:28
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 %><br />
>     <% }%>
> <%= submit_tag %>
> <%= end_form_tag %>
>
> </td>
> <td>
> <p><% if @degree.skills.empty?%>
> (None)
> </p>
> <% 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 %>
> <br />
> <% 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

2. 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

3. 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.
This topic is locked and can not be replied to.