开发者

How do I implement a Rails model named "series"?

开发者 https://www.devze.com 2022-12-18 19:29 出处:网络
The singular and plural forms are the same and I get an undefined_method error when trying to hit the New method.

The singular and plural forms are the same and I get an undefined_method error when trying to hit the New method.

I realize why an开发者_运维问答d I'm aware that the easiest solution would be to use another name.

I'm also aware that I could create a custom inflection, but what?

The problem is that I REALLY need URLs like /series, /series/1 etc because I'm in fact modelling...wait for it...series of events.

Using "set" or "sequence" or some other synonym doesn't convey the intended meaning.

A series of events is a series not a set or sequence.

Is there a way to "alias" a model?

Should/can I use named routes?

Any help is appreciated.


Assuming you used script/generate scaffold series to build your model controller et al, you should have a line in /config/routes.rb like

map.resources :series

If you change it to

map.series_index '/series',:controller=>'series',:action=>:index
map.resource :series

It will work. Or you could add Eric Hill's inflection initializer.


So:

class Series < ActiveRecord::Base
  ...
end

definitely doesn't work? Looking at the source, series is one of the singularization rules built in and according to The Pluralizer, you should be able to name your model class Series as above.


You should be able to do this solely with inflections. In config/initializers/inflections.rb:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable 'series'
end

Restart the app to activate.

0

精彩评论

暂无评论...
验证码 换一张
取 消