Using singular class - SoftwareController

I’m having trouble getting a singular class name to work with rails.
We have a table, software, that lists all the software. We want a
SoftwareController, Software model, and then REST paths like
new_software_path

I’ve tried using the Inflector in config/environment.rb, but neither
of these works:

Inflector.inflections do |inflect|
inflect.uncountable %w(software) # Nope
inflect.irregular ‘software’, ‘software’ # Nope
end

I can only get it to work by explicitly linking to :controller =>
‘software’, and even then it wants to force-load SoftwaresHelper from
helpers/softwares_helper.rb

Any hints?

-Nate

Thinking out loud here:

Assuming that your model is defined like this:

class Software < ActiveRecord::Base

set_table_name “software”

end

Then that will take care of the plural Softwares stuff… I think that
the
RESTFul routing might expect plural controllers… but rails 1.2.2
(released
yesterday) can help solve that problem - see singular resources in the
following post:

http://weblog.rubyonrails.com/2007/2/6/rails-1-2-2-sqlite3-gems-singular-resources

On Feb 7, 7:15 am, “Nate W.” [email protected] wrote:

I’ve tried using the Inflector in config/environment.rb, but neither
of these works:

Inflector.inflections do |inflect|
inflect.uncountable %w(software) # Nope
inflect.irregular ‘software’, ‘software’ # Nope
end

After the inflections, add this line:
ActionController::Routing::Routes.reload

However, also look into Brian’s comment about the singular resources.

Dan M.

A couple of thoughts on this:

Singular resources aren’t useful when you’re using an
Inflector#uncountable object in a has_many relationship. In my case,
I’ve got Personnel. The singular and plural form of Personnel remains
the same, so in config/environment.rb, I set up
Inflector.inflections do |inflect|
inflect.uncountable %w( personnel )
end

then I ran my script/generate scaffold_resource Personnel. After
adding
self.table_name = ‘personnel’
to the model, I still had to monkey the routes around.

In Rails 1.2.1, this works just fine:
map.resources :personnel, :controller => ‘personnel’, :singular =>
‘personnel’

After a mongrel_rails restart, everything was working as expected.

In Rails 1.2.2, this same code falls apart and fails. In my case, I
don’t want to use a singular resource, because the following URL:
http://example.com/personnel
should give me the index action, the list of personnel available.
With singular resource, that URL is mapped to the show action.

  • Jared

then I ran my script/generate scaffold_resource Personnel. After
In Rails 1.2.2, this same code falls apart and fails. In my case, I
don’t want to use a singular resource, because the following URL:
http://example.com/personnel
should give me the index action, the list of personnel available.
With singular resource, that URL is mapped to the show action.

  • Jared

Singular resources are for resources that don’t have index actions.
For instance, an app like Basecamp has some notion of Accounts. Yet,
perhaps you want users to view and modify their own account from
Foo.com. /account maps to the show action and
gets the id value from somewhere else. In this case, it would
probably do something like Account.find_by_name(‘rick’). Other use
cases include:

/settings
/users/5/avatar

etc.

I have no clue if Basecamp does this, I just use that as an example…


Rick O.
http://weblog.techno-weenie.net
http://mephistoblog.com

I’d been hoping to get a good explanation of Singular Resources,
thanks Rick!

On another note, the following does fall apart on Rails 1.2.1:

map.resources :personnel, :controller => ‘personnel’, :singular =>
‘personnel’

Because the url_for helper can’t distinguish between:
personnel_path - Plural, should map to Index
personnel_path(@personnel) - Singular should map to show.

If I’m looking at a ‘show’ page, such as http://example.com/personnel/
1, the following helper
<%= link_to ‘Back’, personnel_path %> still generates
http://example.com/personnel/1,
since it’s picking up the params[:id] value of the current page.

My workaround was to change the singular name, since the plural name
looks singular:
map.resources :personnel, :controller => ‘personnel’, :singular =>
‘person’

and use <%= link_to ‘Show’, person_path(@personnel) %> for all my
singular-style link_to helpers.