0

I have the following hash (@messages) which are messages grouped by sender/from:

{'Tom'=>[#<ShortMessage id: 16, content: "ABC", created_at: "2014-01-28 16:35:33", from: 'Tom'>, #<ShortMessage id: 15, content: "DEF", created_at: "2014-01-28 13:11:20", from: 'Tom'>], 'Jerry'=>[#<ShortMessage id: 5, content: "XYZ", created_at: "2014-01-22 09:48:52", from: 'Jerry'>]}

In my view I loop through the hash to print out the senders name and their group of messages. I've also got a form for each group of messages where the user can reply to that particular sender

<% @messages.each do |sender, message| %>
 <p><%= sender %></p>
 <% message.each do |msg| %>
  <p><%= msg.content %></p>
 <% end %>
// reply form
 <%= form_for @message, remote: true do |f| %>
  <%= f.hidden_field :from, value: from %>
  <%= f.text_field :content %>
  <%= f.submit "Reply" %>
 <% end %>
<% end %>

I'm using ajax and jquery to submit the form and add the new reply text to the end of the group of messages so the page doesn't have to refresh every time a reply is sent

How do I reference which one of the groups of messages to append the new reply to when looping through the hash?

Example if I want to reply to Tom and fill out his reply form how do I append to that group of messages and not Jerry's?

My unsuccessful jquery looks something like this:

<script>
 $('#container').on('ajax:success', function(event, data){
 if (data.succ) {
  // handle your success somehow, like show a new field
 } else { 
  // something went wrong
 }
})
</script>

1 Answer 1

1

I would probably bind ajax:success to a form itself rather than #container (which is not in the question btw). Then t is as simple as

$(this).before(...)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.