Rails3 で date_select を日本語表記(****年**月**日)で表示させたい

少しぐぐると date_select_jp というヘルパーを作るべし!というページがヒットするけど、form_for にねじこみたいのでその方法を調べた。

以下のサイトで行なっている方法で可能だったが gsub 内のスラッシュがエスケープが行われていないためかエラーになったので書き換えた。

*Kirijinri*:Railsのdate_select

最終的には下記のように変更した。

これを書く場所はどこがいいんですかね?

[ruby]

module ActionView

module Helpers

module FormHelper

def date_select_jp(object_name, method, options={ })

options[:use_month_numbers] = true unless options[:use_month_numbers]

t = date_select(object_name, method, options)

if options[:discard_day]

# t.gsub(/</select>(.+?)</select>/m, "</select>年1</select>月")

t.sub(/<\/select>(.+?)<\/select>/m, "</select>年\\1</select>月")

else

#t.gsub(/</select>(.+?)</select>(.+?)</select>/m, "</select>年1</select>月2</select>日")

t.sub(/<\/select>(.+?)<\/select>(.+?)<\/select>/m, "</select>年\\1</select>月\\2</select>日")

end

end

end

class FormBuilder

def date_select_jp(method, options = {})

@template.date_select_jp(@object_name, method, options.merge(:object => @object))

end

end

end

end

[/ruby]

内容は正規表現で置換しているだけ(たぶん)だと思うのですが、超めんどくさいですね、これ。datetime_separator とかすごくおしい気がするのですが、んーまーいっか。

また、form_for 内で html_safe の使い方が分からなかったけど、以下のように記述したらうまく表示されたので記載しておく。

[ruby]

<%= f.date_select_jp(:date, :use_month_numbers => true, :start_year => 1970).html_safe %>

[/ruby]

この時点でRails大変とか言ったらけしからんなんだと思うけどRails大変(*´ω`*)