how to use number_to_currency in controller
1 include ActionView::Helpers::NumberHelper
1 include ActionView::Helpers::NumberHelper
1 def flash_messages 2 %w(notice warning error).each do |msg| 3 concat content_tag(:div, content_tag(:p, flash[msg.to_sym]), :class => msg) unless flash[msg.to_sym].blank? 4 end 5 end
rails 2.3.4 doen’t have i18n support in labels
http://lawrencesong.net/2009/04/i18n-label-in-rails-monkey-patch/
1 module ActionView 2 module Helpers 3 class InstanceTag 4 def to_label_tag_with_i18n(text = nil, options = {}) 5 text ||= object.class.human_attribute_name(method_name) if object.class.respond_to?(:human_attribute_name) 6 7 to_label_tag_without_i18n(text, options) 8 end 9 10 alias_method_chain :to_label_tag, :i18n 11 end 12 end 13 end
1 # in application_helper 2 3 def snippet(text, wordcount, omission) 4 text.split[0..(wordcount-1)].join(" ") + (text.split.size > wordcount ? " " + omission : "") 5 end 6 7 8 # example 9 10 snippet(@post.body, 50, "#{link_to "More...", @post}")
create this helper and uses link_unless_current_controller
1 def current_action?(options) 2 url_string = CGI.escapeHTML(url_for(options)) 3 params = ActionController::Routing::Routes.recognize_path(url_string, :method => :get) 4 params[:controller] == @controller.controller_name && params[:action] == @controller.action_name 5 end 6 7 def current_controller?(options) 8 url_string = CGI.escapeHTML(url_for(options)) 9 params = ActionController::Routing::Routes.recognize_path(url_string, :method => :get) 10 params[:controller] == @controller.controller_name 11 end
1 class TabHelper 2 attr_reader :html, :tabs 3 4 def initialize(template, states) 5 @template = template 6 @states = states 7 @html = [] 8 @tabs = {} 9 end 10 11 def add(action, text) 12 url = { :action => action } 13 html = 14 if @template.request.path.sub(/\?.*/, '') == @template.url_for(url) 15 @states[:active].call(text, url) 16 else 17 @states[:inactive].call(text, url) 18 end 19 @tabs[action] = html 20 @html << html 21 end 22 alias_method :[]=, :add 23 24 def [](*args) 25 @tabs.values_at(*args) 26 end 27 end 28 29 # example 30 31 module ProductsHelper 32 def subnav_links 33 t = TabHelper.new(self, 34 :active => Proc.new {|text, url| %|<div class="current tab">#{text}</div>| }, 35 :inactive => Proc.new {|text, url| %|<div class="tab">#{link_to text, url}</div>| } 36 ) 37 t[:index] = 'Manage Products' 38 t[:front_page] = 'Manage Front Page' 39 40 '<div id="tabs">' + 41 '<div style="float: left">' + 42 t[:index, :front_page].join + 43 '</div>' + 44 '<div class="clear"></div>' + 45 '</div>' 46 end 47 end
http://blog.obiefernandez.com/content/2008/06/railsconf-2008.html
1 def javascript_include_all 2 includes = '' 3 Dir.new("#{RAILS_ROOT}/public/javascripts").each do |js| 4 next if js == "." or js == '..' or !js[".js"] 5 includes += javascript_include_tag(js) + "\n" 6 end 7 end
this helper returns a current url
1 def current_url 2 url_for :only_path => false, :overwrite_params=>{} 3 end
create an application helper to manage your rails messages
concept by nando vieira [simplesideias.com.br]
1 # paste application_helper.rb 2 def flash_message 3 messages = "" 4 [:notice, :info, :warning, :error].each {|type| 5 if flash[type] 6 messages += "<p class=\"#{type}\" id=\"alert\">#{flash[type]}</p>" 7 end 8 } 9 messages 10 end 11 12 #################################### 13 14 # your layout 15 <%= flash_message %> 16 17 <% content_tag :script, :type => "text/javascript" do %> 18 $('alert').style.display = 'none'; 19 new Effect.Appear('alert', {duration: 3}); 20 setTimeout("new Effect.Fade('alert');", 10000); 21 <% end %>