Created
July 14, 2010 04:08
-
-
Save matsuda/475007 to your computer and use it in GitHub Desktop.
Rails' utility helpers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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