0

Problem

My app manages ProjElements, which are subclassed into:

  • Milestone
  • Task
  • Decision
  • ... etc

For a given ProjElement's show.html.erb, you can comment on that project element instance (e.g. you can add a comment on Milestone XYZ or Decision ABC). Like this:

// display project element specific stuff
//  - e.g. show.html.erb for Milestone has milestone-specific stuff
//  - e.g. show.html.erb for Decision has decision-specific stuff

// provide comment functionality
//  - e.g. for Milestone's show.html.erb, code looks like

    <%= form_for [@milestone, Comment.new] do |f| %>
      <% if @milestone.comments.size > 0 %>
         ...
      <% end %>
      <% f.submit %>
    <% end %>

Proposed approach

  • I plan to use a partial for the comment code and use it across the various show.html.erb views for different project elements, as per DRY. But ...

  • How do I write generic code for the partial, the Rails way, so that the partial can deal with different project elements?

lucapette
  • 20,564
  • 6
  • 65
  • 59
Daniel May
  • 2,192
  • 5
  • 25
  • 43

1 Answers1

2

You can pass the element's subclass instance through the locals hash. In app/views/milestones/show.html.erb

render :partial => 'shared/comments', :locals => { :element => @milestone }

In app/views/shared/_comments.html.erb

<% form_for [element, Comment.new] do |f| %>
Tom L
  • 3,389
  • 1
  • 16
  • 14