<%= f.fields_for :ticket_messages do |tm| %>
<% if tm.object.new_record? %>
<%= tm.label :message, "New message" %>
<%= tm.text_area :message, rows: 5, class: 'form-control' %>
<%= f.submit "Update", id: 'submit', class: 'btn btn-primary' %>
<%end%>
<%end%>
<% @ticket.ticket_messages.each do |tm| %>
<% unless tm.new_record? %>
<%# Iterating through the saved ticket_messages, displaying response field above message field.
For a new ticket created by user the response field will be nil until admin responds. %>
<% if tm.response != nil %>
<% cssclass = [] %>
<% if tm.response[/-----BEGIN/] %>
<% cssclass.push 'encrypted' %>
<% else %>
<% cssclass.push 'plaintext' %>
<% end %>
<%= tm.response.split("\r\n").collect{ |line| h line}.join("
").html_safe %>
<%end%>
<% if tm.message != nil %>
<%# When ticket_message created by user, this will always be non-nil, but when admin creates
a ticket_message, this field will be left nil and response field will contain admin's message to user %>
<% cssclass = [] %>
<% if tm.message[/-----BEGIN/] %>
<% cssclass.push 'encrypted' %>
<% else %>
<% cssclass.push 'plaintext' %>
<% end %>
<%= tm.message.split("\r\n").collect{ |line| h line}.join("
").html_safe %>
<%end%>
<%end%><%# new_record? %>
<%end%>