Forum: Ruby on Rails NewbieQ: Redisplaying Categorized Comments

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.
4dfa3542d9933c6fc887dbcea5853a75?d=identicon&s=25 ebrad (Guest)
on 2007-03-10 01:41
(Received via mailing list)
Greetings,
As my first web app with Rails I am creating an HR Tracking program.
There are employee 'records' that can have 'comments' added to each
record.  I also assign each comment a category such as 'general note',
'disciplinary action', or 'job changes' which I've set in my Comments
table using the values either 0, 1, or 2.

I can create the comments fine, but difficulty I am having is
seperating the comments into different areas of the employee record
page based on the category. So, for example, if I want to add a
comment like 'Employee was promoted to manager' and assign it a
category of 'job changes', I would then want that comment to display
in a list below the heading 'Job Changes' on that records 'show' page
just above the comment form.

The best way I can think of is doing something like:
 <%  if comment_type = 1 %>
 <% for comment in @record.comments %>
  <% end %>
  <% end %>
  but I can't seem to get the syntax correct. Can anyone assist? Below
is my code and layout.

-------------------------------------------------------------------------------
APP/CONTROLLERS/RECORDS_CONTROLLER.RB

def comment
    Record.find(params[:id]).comments.create(params[:comment])
    flash[:notice] = "Added your comment."
    redirect_to :controller => "records", :action => "show", :id =>
params[:id]
  end
-------------------------------------------------------------------------------
APP/VIEWS/RECORD/SHOW.RHTML

<fieldset><legend>Job Change Comments</legend>
<% for comment in @record.comments  %>
<span class="smaller">Submitted: <%= comment.created_at %></span><br /
>
<%=h comment.body %><hr>
  <% end %>

<fieldset><legend>Add a Comment</legend>
  <%= form_tag :action => "comment", :id => @record %>
  <%= text_area "comment", "body", "cols" => 40, "rows" => 5 %><br/>
  <label for="record_comment_type">Comment Type</label><br/>
  <%= select( :comment, :comment_type, [['General', 0],
[ 'Disciplinary', 1],['Job Change', 2]]) %><br/>
  <br />
  <%= submit_tag "Save" %>

</fieldset>
-------------------------------------------------------------------------------
COMMENTS TABLE STRUCTURE

id (int)
body (text)
created_at (datetime)
comment_type (int)
record_id (int)
This topic is locked and can not be replied to.