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
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.
My ruby app is connected to an existing DB so the table name has to stay as nas.
Then why do your routes/controllers also have to be named nas
? Once you fixed it on your 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
A guess, maybe you should try overriding the naming convention, because 'nas' is not plural? (assuming that's why the s dropped)
# 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)
In your routes.rb, try,
match '/nas', :to => 'na'
精彩评论