3

First of all a warning: I am new to Ruby on Rails. I am creating an application and run into the problem creating a controller when the entity has the same name in plural and singular. In my case it's "fish", so I have a Fish model, a FishController, a fish table etc. It works just fine (surprisingly, at least to me), except when I try to use the path helper:

<%= link_to 'My fishes', fish_path %>

When I try that, rails shows me an error

No route matches {:action=>"show", :controller=>"fish"} missing required keys: [:id]

I know that when you have a resource :apples, apples_path would refer to the index and apple_path(:id) (without s) to the show method for a particular item. My guess about what is happening is that, when I say fish_controller I am referring to the show method, and hence the error: it is missing an ID.

My question is, how do I call the "plural" fish_path to go to the index?

And a preventive question: do you know of any other issues that I can find related to this "weirdness" with the names?

José Ramón
  • 162
  • 11

1 Answers1

3
  • name for fish#index is fish_index
  • name for fish#show is fish

So you should use:

<%= link_to 'My fishes', fish_index_path %>

And link fish's profile to:

<%= link_to 'Biggest Fish', fish_path(@fish) %>
dimakura
  • 7,575
  • 17
  • 36
  • I'm quite sure that index route helpers are never created with `(subject)_index_path`, but with an argument-less `GET (subject)_path`. In the case of fish and sheep it's rather an inflection problem. For your suggestion to work, he must name that route `get 'fish/': 'fish#index', as: 'fish_index'`. – wiesion Sep 29 '15 at 18:40
  • @wiesion you can examine `rake routes` to see that `fish#index` is named as `fish_index` and it's helper is called accordingly `fish_index_path`. – dimakura Sep 29 '15 at 18:43
  • tbh, i didn't set up a test right now, it's just what i remember by memory: i am quite sure there is no `_index` in index path helpers. So i'll get my laptop and make a quick try :) – wiesion Sep 29 '15 at 18:47
  • @wiesion please don't comment on thing you "don't remember". As to your advice to uncomment `inflect.uncountable %w( fish sheep )`, it's not going to work, because, if you read comment in `inflections.rb` file, it says: "All of these examples are active by default", so you effectively doing nothing by removing this comment. – dimakura Sep 29 '15 at 18:50
  • Just tried, you were one step ahead indeed. In case of an uncountable inflection ActiveSupport does indeed append `_index` - i never ran into such a case and didn't need it, so i was quite sure that by adding the inflection the route helper would work without `_index`. – wiesion Sep 29 '15 at 18:55
  • @dimakura I just tried it and it works like a charm. I googled for hours but I couldn't find this little 'trick' anywhere. Thanks a lot! Beer is on me :) – José Ramón Sep 29 '15 at 21:07