- Most Recent |
24 hours |
7 days |
30 days |
365 days |
railscasts.com —
Sometimes you need to display an administrative announcement to every page on the site and give the users the ability to hide the announcement. See how in this episode. Yet another great railscast. Follow the link and make sure to have a look at our content page for an alternative way to do it.
Posted by
aroedl 8 months ago
aroedl 8 months ago This is how he did it:
Generators
script/generate scaffold announcement message:text starts_at:datetime ends_at:datetime
script/generate controller javascripts
View
<!-- layouts/application.html.erb -->
<% unless current_announcements.empty? %>
<div id="announcement">
<% for announcement in current_announcements %>
<p><%=h announcement.message %></p>
<% end %>
<p><%= link_to_remote "Hide this message", :url => "/javascripts/hide_announcement.js" %></p>
</div>
<% end %>
Model/Helper/Controller/RJS/Route
# models/announcement.rb
def self.current_announcements(hide_time)
with_scope :find => { :conditions => "starts_at <= now() AND ends_at >= now()" } do
if hide_time.nil?
find(:all)
else
find(:all, :conditions => ["updated_at > ?", hide_time])
end
end
end
# application_helper.rb
def current_announcements
@current_announcements ||= Announcement.current_announcements(session[:announcement_hide_time])
end
# javascripts_controller.rb
def hide_announcement
session[:announcement_hide_time] = Time.now
end
# hide_announcement.js.rjs
page[:announcement].hide
# routes.rb
map.connect ":controller/:action.:format"
This is how you could do it
Plugin installation
script/plugin install http://pmade.com/svn/oss/stickies/trunk
Somewhere in your application layout:
<%= render_stickies %>
Application controller
notice_stickie("This is an announcement!")
Please log in or sign up and vote for this trick if it was helpful for you.
Don't forget to subscribe to our
RSS/Atom feed to get the latest tricks.
Don't forget to subscribe to our
RSS/Atom feed to get the latest tricks.









