0

I got

render :text => "Some text"

what I really need is something like this

render :text => "Some text", :duration => 5000

The "some text" will be rendered for a short duration of time and then it will disappear.

If it is not possible, can I use :alert tag for the same thing, and how exactly?

4

1 回答 1

2

You have to consider that you are using HTTP, which is stateless. This means: There is NO connection between client and server. It's always just:

  • Client: Hello server, please give me http://.../someResource
  • Server: Here it is

Every time the client asks for a resource the server cannot remember that he was there before (at least in plain strict HTTP). Then there came people and invented Sessions and Cookies, but that's a different story.

What I wanted to say: The server can ONLY send the requested resource and after this is done he doesn't know that there was a client and requested something.

You have only one choice for dynamic behaviour: JavaScript (and since CSS3, it should also be possible to trigger an animation that hides some div after 5000ms).

Now you have to consider:

  • Should the client itself hide the alert box? (that's the most common and most easy way using jQuery)
  • Should the server tell the client to hide it? (I know I said above that's not possible, but it actually is using something called server push, which is implemented in a library called atmosphere (formerly comet). But it's not a real message from the server, it's more a second request from the client which first gets answered by the server when there's an event to send to the client (long-pooling)). Notice: This also required JavaScript on the client side.

I'd say: Go with the first solution (jQuery) 'cause it's already built into Rails (at least if you are at Rails 3.1 or above)

Have a look here: How to hide a div after some time period?

于 2013-02-27T13:48:59.793 回答