困惑,要使用的原型帮手 [英] Confused as to which Prototype helper to use
问题描述
看完 http://api.rubyonrails.org/classes/ActionView/助手/ PrototypeHelper.html我只是似乎无法找到我要找的东西。我有删除最早的消息后,消息列表达到24一个简单的模型,该模型是这样的简单:
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
有低于用于添加新的消息的消息的列表中的消息的形式。 我使用的原型/ RJS新信息添加到列表的顶部。 create.rjs:
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
我的index.html.erb是很简单的:
My index.html.erb is very simple:
<div id="messages">
<%= render :partial => @messages %>
</div>
<%= render :partial => "message_form" %>
在添加新的消息出现得很好,但是当24消息已达到限制它只是不断增加消息,并且不会删除旧的。理想情况下,我想他们淡出作为新加入的,但他们可以消失。注释行的create.rjs实际工作,它消除了过期的消息,但我加入了新的消息时失去了视觉效果。有没有人对如何实现从这个简单的列表中添加和删除信息与这两种效应的建议?帮助将不胜AP preciated。感谢您的阅读。 PS:将periodically_call_remote在这种情况下,有用吗?
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?
推荐答案
我会假设你的的消息
部分看起来像这样一旦它被呈现:
I'm going to assume that your message
partial looks something like this once it's been rendered:
<div id='message_123'>
<h3>Message Header</h3>
<p>This is the body of the message</h3>
</div>
如果不是这种情况下,改变它,以便它是。应该被包裹在一个每个消息的div
或范围
已在它的消息的ID。然后,您可以添加下面的方法到你的消息
型号:
If this isn't the case, change it so it is. Each message should be wrapped in a div
or span
that has the ID of that message in it. Then you can add the following method to your Message
model:
def old_messages
messages = Message.all(:order => 'updated_at DESC')
if messages.size >= 24
return messages[24..-1]
else
return []
end
end
然后,在响应该请求的动作,只需添加一行:
Then, in the action that responds to this request, simply add the line:
@old_messages = Message.old_messages
和改变你的RJS是:
page.insert_html :top, :messages, :partial => @message
page[@message].visual_effect :grow
#page[dom_id(@messages)].replace :partial => @message
page[:message_form].reset
@old_messages.each do |m|
page.remove(m.id)
end
您可以看到,我使用的轨道<一href="http://api.rubyonrails.org/classes/ActionView/Helpers/PrototypeHelper/JavaScriptGenerator/GeneratorMethods.html#M001668"相对=nofollow>删除方法来完成的采取不受欢迎的邮件进入了DOM的行为。
You can see that I'm using the rails remove method to accomplish the act of taking the unwanted messages out of the DOM.
这篇关于困惑,要使用的原型帮手的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!