我有一个页面,用户可以在当前的线程对话中看到消息列表。在rails上一次更新多个属性
他们可以使用删除链接(每个消息的每个循环中的链接)删除每条消息:
<%= link_to 'Delete', message, :method => :delete, :confirm => "Are you sure?", :title => message.body %> <br />
这基本上将它们连接到我的messages_controller一个动作:
def destroy
@message = Message.find(params[:id])
@message.update_attribute('sender_status', 1) if @message.sender_id == current_user.id
@message.update_attribute('recipient_status', 1) if @message.recipient_id == current_user.id
@message.destroy if @message.sender_status == 1 && @message.recipient_status == 1
flash[:success] = "Message deleted"
redirect_to :back
end
我现在想要的是所有消息之前的一个链接(所以我不会把它放在每个块中)。该链接将被称为“全部删除”,而不是使用值1更新1属性,它会将所有属性(sender/recipient_status)更新为1,以便用户不必单击每个单独的消息删除链接。
我有点卡住如何做到这一点。我不使用复选框,而是删除链接,以便删除所有我宁愿使用链接。
任何机构都可以为我提供解决方案吗?我真的很感激它。以为我昨天晚上睡觉,并在早上有一个想法,但仍然没有想到。
亲切的问候
更新
这些消息属于交谈。我可以通过以下方式获取对话: 1)MessageThread表。这将会话中第一条消息的message_id存储为该对话中其他消息的parent_id。举个例子。
+-----+----------+---------+---------+---------+--------+---------+----------+----------+----------+
| id | sende... | reci... | body | pare... | status | crea... | updat... | sende... | recip... |
+-----+----------+---------+---------+---------+--------+---------+----------+----------+----------+
| 220 | 4 | 1 | hi | 220 | 0 | 2012... | 2012-... | 0 | 0 |
| 221 | 1 | 4 | hey | 220 | 0 | 2012... | 2012-... | 0 | 0 |
| 222 | 4 | 1 | hi | 220 | 0 | 2012... | 2012-... | 0 | 0 |
| 223 | 1 | 4 | hi | 220 | 0 | 2012... | 2012-... | 0 | 0 |
| 232 | 1 | 4 | good | 220 | 0 | 2012... | 2012-... | 0 | 0 |
| 233 | 4 | 1 | and ... | 220 | 0 | 2012... | 2012-... | 0 | 0 |
| 237 | 1 | 4 | hmm | 220 | 0 | 2012... | 2012-... | 0 | 0 |
| 238 | 1 | 4 | yay | 220 | 0 | 2012... | 2012-... | 0 | 0 |
| 239 | 1 | 4 | yess | 220 | 0 | 2012... | 2012-... | 0 | 0 |
2)我也可以通过父母id找到对话。例如m = Message.find_by_parent_id(220)。
获得在康沃其他消息我只需要输入m.children
这里是我的messagethreads表的外观:
+-----+------------+-----------+--------------+--------+---------------------+---------------------+
| id | message_id | sender_id | recipient_id | status | created_at | updated_at |
+-----+------------+-----------+--------------+--------+---------------------+---------------------+
| 102 | 247 | 1 | 5 | 0 | 2012-02-16 16:18... | 2012-02-16 16:18... |
| 101 | 246 | 1 | 3 | 0 | 2012-02-16 16:14... | 2012-02-16 16:14... |
| 90 | 220 | 4 | 1 | 0 | 2012-02-15 14:05... | 2012-02-15 14:05... |
| 89 | 219 | 4 | 38 | 0 | 2012-02-15 12:26... | 2012-02-15 12:26... |
| 88 | 218 | 2 | 4 | 0 | 2012-02-14 13:41... | 2012-02-14 13:41... |
| 87 | 210 | 1 | 2 | 0 | 2012-02-14 13:31... | 2012-02-14 13:31... |
+-----+------------+-----------+--------------+--------+---------------------+---------------------+
这里是为我工作:
def destroy_all_messages
Message.find_by_parent_id(params[:format]).children.each do |message|
message.update_attribute('sender_status', 1) if message.sender_id == current_user.id
message.update_attribute('recipient_status', 1) if message.recipient_id == current_user.id
message.destroy if message.sender_status == 1 && message.recipient_status == 1
end
flash[:success] = "Messages deleted"
redirect_to :back
end
和:
<%= link_to 'Delete All', messages_delete_all_messages_path(@current_thread.message_id), :method => :delete, :confirm => "Are you sure?" %> <br />
这是我的工作..意见? 我注意到我不得不使用“params [:format]”来获取我传递给控制器中link_to消息路径的参数。
我想应该有一个与当前对话线程相关的实体,例如“对话”记录。如果这样传递会话记录ID,找出要删除的会话的所有消息。 – nkm 2012-02-17 12:10:14
给我的信息添加了更多信息 – LondonGuy 2012-02-17 12:17:50