プログラミング

rails taxがtaxesではなくtaxisになってしまう

2013年6月27日

rails generate scaffold tax rate:decimal{3-2} started_on:date

などとして消費税マスタを用意していたのですが、routingやscaffoldしたファイル内でtaxがtaxisに変換されてしまっています。

rake routes

(snip..)
taxes GET /taxes(.:format) taxes#index
POST /taxes(.:format) taxes#create
new_taxis GET /taxes/new(.:format) taxes#new
edit_taxis GET /taxes/:id/edit(.:format) taxes#edit
taxis GET /taxes/:id(.:format) taxes#show
PUT /taxes/:id(.:format) taxes#update
DELETE /taxes/:id(.:format) taxes#destroy
(snip..)

隣のエンジニアに教えてもらった、

【Rails】複数形と単数形がおかしい - うみやま亭 http://sea-mountain.hatenablog.jp/entry/20110621/1308645941

が参考になりました。

config/initializers/inflections.rbに変換ルールを書けるようで、

ActiveSupport::Inflector.inflections do |inflect|
  inflect.irregular 'tax', 'taxes'
end

と書いておけば、

rake routes

(snip..)
taxes GET /taxes(.:format) taxes#index
POST /taxes(.:format) taxes#create
new_tax GET /taxes/new(.:format) taxes#new
edit_tax GET /taxes/:id/edit(.:format) taxes#edit
tax GET /taxes/:id(.:format) taxes#show
PUT /taxes/:id(.:format) taxes#update
DELETE /taxes/:id(.:format) taxes#destroy
(snip..)

となり、期待通りです。

あとはscaffoldしたファイル内のtaxisをtaxに手で置換すればオッケーです。

-プログラミング
-