开发者

Rails inserting javascript as a flash notice or message?

I'm wondering if this is a good idea or not, either way I'd like you guys opinion on how to best achieve this sort of messaging popup system I have in my app.

Under some occasio开发者_开发知识库ns I want to popup a lightbox for users when certain actions are triggered. I already have the lightbox working and opening when when my controller returns JS for a request.

Here is the senario, I want to check if a user has new messages when a new request is made, and if they do I want to show the messages in my lightbox when the new page is loaded.

Should I just put some JS at the bottom of my <body> and render it if the user has messages? Or should I use like flash[:notice] and have it render as JS or something... I'm a bit stuck you see.


Don't use flash notices, this is not what they are for at all. I would have something in the layout like this:

<% if (messages = current_user.new_messages).size > 0 %>
  <%= javascript_tag "display_messages(#{messages.collect(&:message_text).inspect})" %>
<% end %> 

obviously here i'm guessing at your messages' methods but you get the idea. .inspect will give it an array of strings, you could give it the message data as a json object or whatever.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜