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 %> <%= link_to ''Remove'', {:action => ''remove_skill'', :id=> @degree, :which_skill => skill.id}, :confirm => ''Are you sure?'', :post => true %> <br /> <% end %> <% end %> --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 %> <%= 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 <m.davis-pyHcBy/D9uhaa/9Udqfwiw@public.gmane.org> 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 %> > <%= link_to ''Remove'', {:action => ''remove_skill'', > :id=> @degree, :which_skill => skill.id}, > :confirm => ''Are you sure?'', :post => true %> > <br /> > <% end %> > <% end %> > > > > >-- Obie Fernandez http://jroller.com/obie/ Pre-order my book The Rails Way today! http://www.amazon.com/dp/0321445619 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 %> > <%= 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. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Apparently Analagous Threads
- [LLVMdev] problem trying to write an LLVM register-allocation pass
- Question about setting field values for a belongs_to model
- Pay rates for Rails developers
- editing multiple models in one form
- Any liberally-licensed open source projects out there that make good use of RSpec?