困惑于使用哪个原型助手
阅读 http://api.rubyonrails.org/classes/ActionView/Helpers/ 后PrototypeHelper.html 我似乎找不到我要找的东西。我有一个简单的模型,在消息列表达到 24 条后删除最旧的消息,该模型很简单:
class Message < ActiveRecord::Base
after_create :destroy_old_messages
protected
def destroy_old_messages
messages = Message.all(:order => 'updated_at DESC')
messages[24..-1].each {|p| p.destroy } if messages.size >= 24
end
end
消息列表下方有一个消息表单,用于添加新消息。 我正在使用 Prototype/RJS 将新消息添加到列表顶部。 create.rjs:
page.insert_html :top, :messages, :partial => @message
page[@message].visual_effect :grow
#page[dom_id(@messages)].replace :partial => @message
page[:message_form].reset
我的index.html.erb非常简单:
<div id="messages">
<%= render :partial => @messages %>
</div>
<%= render :partial => "message_form" %>
当添加新消息时,它们看起来很好,但是当达到24条消息限制时,它只会继续添加消息并且不会删除旧消息。理想情况下,我希望它们随着新的添加而淡出,但它们可能会消失。 create.rjs 中的注释行实际上有效,它删除了过期的消息,但在添加新消息时我失去了视觉效果。有谁对如何完成从这个简单列表中添加和删除消息并对两者都有影响的建议有建议吗?帮助将不胜感激。感谢您的阅读。 PS:在这种情况下periodically_call_remote会有帮助吗?
After reading http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper.html I just can't seem to find what I'm looking for. I have a simplistic model that deletes the oldest message after the list of messages reaches 24, the model is this simple:
class Message < ActiveRecord::Base
after_create :destroy_old_messages
protected
def destroy_old_messages
messages = Message.all(:order => 'updated_at DESC')
messages[24..-1].each {|p| p.destroy } if messages.size >= 24
end
end
There is a message form below the list of messages which is used to add new messages.
I'm using Prototype/RJS to add new messages to the top of the list.
create.rjs:
page.insert_html :top, :messages, :partial => @message
page[@message].visual_effect :grow
#page[dom_id(@messages)].replace :partial => @message
page[:message_form].reset
My index.html.erb is very simple:
<div id="messages">
<%= render :partial => @messages %>
</div>
<%= render :partial => "message_form" %>
When new messages are added they appear just fine, but when the 24 message limit has been reached it just keeps adding messages and doesn't remove the old ones. Ideally I'd like them to fade out as the new ones are added, but they can just disappear. The commented line in create.rjs actually works, it removes the expired message but I lose the visual effect when adding a new message. Does anyone have a suggestion on how to accomplish adding and removing messages from this simple list with effects for both? Help would be greatly appreciated. Thanks for reading. P.S.: would periodically_call_remote be helpful in this situation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我将假设您的
message
部分在渲染后看起来像这样:如果不是这种情况,请更改它。每条消息都应包装在包含该消息 ID 的
div
或span
中。然后,您可以将以下方法添加到您的Message
模型中:然后,在响应此请求的操作中,只需添加以下行:
并将您的 RJS 更改为:
您可以看到我正在使用Rails remove 方法来完成该操作从 DOM 中取出不需要的消息。
I'm going to assume that your
message
partial looks something like this once it's been rendered:If this isn't the case, change it so it is. Each message should be wrapped in a
div
orspan
that has the ID of that message in it. Then you can add the following method to yourMessage
model:Then, in the action that responds to this request, simply add the line:
And change your RJS to be:
You can see that I'm using the rails remove method to accomplish the act of taking the unwanted messages out of the DOM.
也许这些步骤会有所帮助。
并且您有一个可行的解决方案。我希望这有帮助。
Maybe these steps can help.
And you have a working solution. I hope this helps.