NewbieQ: Redisplaying Categorized Comments


#1

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

Job Change Comments <% for comment in @record.comments %> Submitted: <%= comment.created_at %>
<%=h comment.body %>
<% end %> Add a Comment <%= form_tag :action => "comment", :id => @record %> <%= text_area "comment", "body", "cols" => 40, "rows" => 5 %>
Comment Type
<%= select( :comment, :comment_type, [['General', 0], [ 'Disciplinary', 1],['Job Change', 2]]) %>

<%= submit_tag "Save" %>

COMMENTS TABLE STRUCTURE

id (int)
body (text)
created_at (datetime)
comment_type (int)
record_id (int)