views:

115

answers:

3

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 and 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.

A: 

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.

John Topley
Definitely doesn't work!
Danger Angell
A: 

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.

Eric Hill
"series" is already one of the rules built in to the Inflector.
John Topley
Hitting http://localhost:3000/series I get:ActiveRecord::RecordNotFound in SeriesController#showCouldn't find Series without an ID
Danger Angell
Ok, I had a type in my routes file...fixed it and tried again using the inflection you specified and I'm getting:ActionController::RoutingError in Series#newShowing app/views/series/new.html.erb where line #3 raised:series_url failed to generate from {:action=>"show", :controller=>"series"} - you may have ambiguous routes, or you may need to supply additional parameters for this route. content_url has the following required parameters: ["series", :id] - are they all satisfied?
Danger Angell
Mea culpa on the existing inflector. It's kind of old (2008), but this article seems to indicate that 'series' may be an unreserved reserved word in AR: http://www.elfsternberg.com/2008/07/15/compromising-with-rails-the-inflector-and-the-plural-of-series/
Eric Hill
Also, I was able to reproduce the issue in a test app with only a series scaffold. Bringing up #index worked fine, but it choked when I tried to create a new series, strangely returning a #show error.
Eric Hill
I knew I wasn't crazy :-)Still haven't found a solution.Might have to settle for a less than desireable model name.
Danger Angell
+1  A: 

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.

BaroqueBobcat
When I do this I'm getting:NoMethodError in Series#indexShowing app/views/series/index.html.erb where line #9 raised:You have a nil object when you didn't expect it!You might have expected an instance of Array.The error occurred while evaluating nil.each
Danger Angell
What code do you have there? in the raw scaffold, that is just a link to Show.
BaroqueBobcat
I generated the scaffold, raked the db, edited routes.rb per your suggestion, restarted the server and hit /series and got that error. Didn't edit/change anything else.
Danger Angell
is the error on something like `@series.each`? In my scaffold generation there isn't an each call on something.What version of rails are you using?Are you using any special plugins?Etc?
BaroqueBobcat