Skip to content

Instantly share code, notes, and snippets.

@matsuda
Created July 14, 2010 04:08
Show Gist options
  • Save matsuda/475007 to your computer and use it in GitHub Desktop.
Save matsuda/475007 to your computer and use it in GitHub Desktop.
Rails' utility helpers
module ApplicationHelper
#
# JavaScriptコードを<head>タグに挿入する
#
def content_for_javascript_tag(content_or_options_with_block = nil, html_options = {}, &block)
content_for :javascript do
javascript_tag(content_or_options_with_block, html_options, &block)
end
end
def simple_format(text)
return text if text.blank?
st = text.to_s.dup
st = st.gsub(/(\r\n|\n|\r)/, "\n")
st = st.gsub(/\n/, '<br />')
st
end
#
# htmlオプション
#
def html_id_and_name(object_name, column_name, idx = nil, nested_object_name = nil)
{
:id => html_id(object_name, column_name, idx, nested_object_name),
:name => html_name(object_name, column_name, idx, nested_object_name)
}
end
def html_id(object_name, column_name, idx = nil, nested_object_name = nil)
i = idx ? "_#{idx}" : ''
nested = nested_object_name ? "_#{nested_object_name}" : ''
%Q{#{object_name}#{nested}_#{column_name}#{i}}
end
def html_name(object_name, column_name, idx = nil, nested_object_name = nil)
i = idx ? "[#{idx}]" : ''
nested = nested_object_name ? "[#{nested_object_name}]" : ''
%Q{#{object_name}#{nested}#{i}[#{column_name}]}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment