you are here: codestackercodes [RSS] → tag helper [RSS]

how to use number_to_currency in controller

include ActionView::Helpers::NumberHelper

flash_messages helper method to rails 3

def flash_messages 
  %w(notice warning error).each do |msg| 
    concat content_tag(:div, content_tag(:p, flash[msg.to_sym]), :class => msg) unless flash[msg.to_sym].blank? 
  end 
end

i18n in labels

rails 2.3.4 doen't have i18n support in labels

http://lawrencesong.net/2009/04/i18n-label-in-rails-monkey-patch/

module ActionView 
  module Helpers 
    class InstanceTag 
      def to_label_tag_with_i18n(text = nil, options = {}) 
        text ||= object.class.human_attribute_name(method_name) if object.class.respond_to?(:human_attribute_name) 
 
        to_label_tag_without_i18n(text, options) 
      end 
 
      alias_method_chain :to_label_tag, :i18n 
    end 
  end 
end

truncate text at a word boundry

# in application_helper


def snippet(text, wordcount, omission)
 text.split[0..(wordcount-1)].join(" ") + (text.split.size > wordcount ? " " + omission : "")
end


# example


snippet(@post.body, 50, "#{link_to "More...", @post}")

current_action and current_controller helpers

create this helper and uses linkunlesscurrent_controller

def current_action?(options)
  url_string = CGI.escapeHTML(url_for(options))
  params = ActionController::Routing::Routes.recognize_path(url_string, :method => :get)
  params[:controller] == @controller.controller_name && params[:action] == @controller.action_name
end

def current_controller?(options)
  url_string = CGI.escapeHTML(url_for(options))
  params = ActionController::Routing::Routes.recognize_path(url_string, :method => :get)
  params[:controller] == @controller.controller_name
end

simple tabs helper

class TabHelper
  attr_reader :html, :tabs

  def initialize(template, states)
    @template = template
    @states = states
    @html = []
    @tabs = {}
  end

  def add(action, text)
    url = { :action => action }
    html = 
      if @template.request.path.sub(/\?.*/, '') == @template.url_for(url)
        @states[:active].call(text, url)
      else
        @states[:inactive].call(text, url)
      end
    @tabs[action] = html
    @html << html
  end
  alias_method :[]=, :add
  
  def [](*args)
    @tabs.values_at(*args)
  end
end

# example


module ProductsHelper
  def subnav_links
    t = TabHelper.new(self,
      :active   => Proc.new {|text, url| %|<div class="current tab">#{text}</div>| },
      :inactive => Proc.new {|text, url| %|<div class="tab">#{link_to text, url}</div>| }
    )
    t[:index] = 'Manage Products'
    t[:front_page] = 'Manage Front Page'
    
    '<div id="tabs">' +
      '<div style="float: left">' +
        t[:index, :front_page].join +
      '</div>' +
      '<div class="clear"></div>' +
    '</div>'
  end
end

javascript_include_all

http://blog.obiefernandez.com/content/2008/06/railsconf-2008.html

  def javascript_include_all
    includes = ''
    Dir.new("#{RAILS_ROOT}/public/javascripts").each do |js|
      next if js == "." or js == '..' or !js[".js"]
      includes += javascript_include_tag(js) + "\n"
    end
  end

current_url

this helper returns a current url

  def current_url
    url_for :only_path => false, :overwrite_params=>{}  
  end

new flash helper with prototype effects

create an application helper to manage your rails messages
concept by nando vieira [simplesideias.com.br]

  # paste application_helper.rb

  def flash_message 
    messages = ""
    [:notice, :info, :warning, :error].each {|type|
      if flash[type]
        messages += "<p class=\"#{type}\" id=\"alert\">#{flash[type]}</p>"
      end
    }    
    messages
  end

  ####################################


  # your layout 

  <%= flash_message %>
	
  <% content_tag :script, :type => "text/javascript" do %>
    $('alert').style.display = 'none';
    new Effect.Appear('alert', {duration: 3});
    setTimeout("new Effect.Fade('alert');", 10000);
  <% end %>
Displaying all 9 Code