[英]How to identify hash elements in a loop when using jquery
我有以下哈希(@messages),它們是按發件人/發件人分組的消息:
{'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'>]}
在我看來,我遍歷散列以打印出發件人姓名及其消息組。 我還為每組郵件提供了一個表格,用戶可以在其中答復特定的發件人
<% @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 %>
我正在使用ajax和jquery提交表單並將新的回復文本添加到消息組的末尾,因此不必每次發送回復時刷新頁面
遍歷哈希時,如何引用附加新回復的消息組中的哪一組?
例如,如果我想回復湯姆並填寫他的回復表格,我該如何附加到那組消息而不是傑里的消息上?
我不成功的jquery看起來像這樣:
<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>
我可能會將ajax:success
綁定到表單本身,而不是#container
(btw不在問題中)。 那么t就像
$(this).before(...)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.