Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails3 Controllers Plural / Singular Routes

I have some issues with a controller in my rails3 application that is called nas

My ruby app is connected to an existing DB so the table name has to stay as nas.

In my models, I have previously been able to do this:

set_table_name

But I don't know how to do this in my controller / routes.

Right now, my routes contains this:

resources :nas

And the output is:

        new_na GET    /nas/new(.:format)               {:action=>"new", :controller=>"nas"}
       edit_na GET    /nas/:id/edit(.:format)          {:action=>"edit", :controller=>"nas"}
            na GET    /nas/:id(.:format)               {:action=>"show", :controller=>"nas"}
               PUT    /nas/:id(.:format)               {:action=>"update", :controller=>"nas"}
               DELETE /nas/:id(.:format)               {:action=>"destroy", :controller=>"nas"}

As you can see, rails drops the 's'

How can I resolve this?

Thanks

like image 318
Jenny Blunt Avatar asked Feb 23 '23 18:02

Jenny Blunt


1 Answers

It's pretty confusing because I have no idea what a "na" or "nas" is. From your question I have the idea that you always want to refer to it as "nas", both plural and singular.

If that's the case, then the answer is to put this in config/initializers/inflections.rb:

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable "nas"
end

This will also make your Nas model use the nas table by default, so no need for set_table_name.

However note that there is no reason to use Nas for your controllers if you don't want to! You can name them anything you like, as long as this is reflected in routes.rb and you use the correct model in your controller.

like image 67
molf Avatar answered Mar 08 '23 11:03

molf