5

I have the following line in my routes.rb (Rails 4.1.4):

resources :request_caches

However, when I run rake routes I get the following output:

request_caches    GET    /request_caches(.:format)            request_caches#index
                  POST   /request_caches(.:format)            request_caches#create
new_request_cach  GET    /request_caches/new(.:format)        request_caches#new
edit_request_cach GET    /request_caches/:id/edit(.:format)   request_caches#edit
request_cach      GET    /request_caches/:id(.:format)        request_caches#show
                  PATCH  /request_caches/:id(.:format)        request_caches#update
                  PUT    /request_caches/:id(.:format)        request_caches#update
                  DELETE /request_caches/:id(.:format)        request_caches#destroy

As you can see, Rails somehow maps request_caches plural to request_cach singular. But it should be request_cache. Is this some kind of special case, because of the word caches? I've also played around with

resources :request_caches, as: :request_cache

But this results in wrong routes like request_cache_index. And furthermore, I think this is a standard task and should be solved clearly using Rails intern route helpers.

So, what am I doing wrong?

23tux
  • 14,104
  • 15
  • 88
  • 187
  • what if you do this `resources :request_caches, :path => "request_cache"` i think it will work – Athar Aug 04 '15 at 15:00
  • 2
    I don't know the reason for it but you can achieve it with putting `inflect.irregular 'request_cache', 'request_caches'` in `config/initializers/inflections.rb`. – Pavan Aug 04 '15 at 15:01
  • If you don't worry about why it is generating `request_cach` and just looking for a solution, then I can post my comment as an answer :) – Pavan Aug 04 '15 at 15:09

3 Answers3

11

Rails guesses. It's not perfect. In config/initializers/inflections.rb add

ActiveSupport::Inflector.inflections(:en) do |inflect|
   inflect.irregular 'request_cache', 'request_caches'  
end

You'll need to restart the server as it's in an initializer.

j-dexx
  • 10,286
  • 3
  • 23
  • 36
  • thx, I've accepted your answer, first come, first serve ;) but thanks to the others as well! – 23tux Aug 04 '15 at 15:37
2

Have a look at config/initializers/inflections.rb. There should be some examples in the comments.

Something like this should do the trick:

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.singular 'request_caches' 'request_cache'
end

Be sure to restart the server after making changes to an initializer.

amiuhle
  • 2,673
  • 1
  • 19
  • 28
2

As I said,you can achieve it by changing config/initializers/inflections.rb like below

ActiveSupport::Inflector.inflections(:en) do |inflect|
  inflect.irregular 'request_cache', 'request_caches'
end
Pavan
  • 33,316
  • 7
  • 50
  • 76