Search code examples
ruby-on-railsrubypluralize

Rails path helper with singular controller names


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?


Solution

    • 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) %>