Rails3 Controllers Multiple / Singular Routes

I have some problems with the controller in my rails3 application called nas

My ruby ​​app connects to an existing database, so the table name should remain as nas.

In my models, I could previously do this:

set_table_name 

But I do not know how to do this in the controller / routes.

Right now, my routes contain this:

 resources :nas 

And the result:

  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, the rails dump 's'

How can i solve this?

thanks

+4
source share
4 answers

This is pretty confusing because I have no idea what 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 this is 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 default Nas table, so there is no need for set_table_name .

However, note that it makes no sense to use Nas for your controllers if you do not want to! You can call them anything if reflected in routes.rb and you use the correct model in your controller.

+4
source

Guess maybe you should try redefining the naming convention because "nas" is not plural? (assuming why s fell)

 # Inflection rule Inflector.inflections do |inflect| inflect.irregular 'nas', 'nases' end 

in environment.rb

Edit: Instead of environment.rb use: config/initializers/inflections.rb (thanks Benoit Garret )

+2
source

My ruby ​​app connects to an existing database, so the table name should remain as nas.

Then why should your routes / controllers also be named nas ? Once you have fixed it at the model level, everything should be fine.

 # model.rb class WhateverILikeToCallMyModel set_table_name "nas" end # controller.rb class WaynesController << ApplicationController # ... def index @items = WhateverILikeToCallMyModel.all end end # routes.rb resources :waynes 
+2
source

In your route.rb, try,

 match '/nas', :to => 'na' 
0
source

All Articles