Provide a hint when unable to pluralize a route?

So one thing I had a WTF? with when I ran into it a few months ago was what routes do when a model name can’t be pluralized. For example, if you have a model named Series. series is both the singular and plural form of the word series, so whereas you’d normally have path methods like book_path and books_path, with Series you get series_path and series_index_path.

If you try to use series_path without an id, you just get an error about the missing argument, and it’s not clear that Rails has generated a series_index_path - which is what you should be using to link to the index.

I’m not sure how realistic or performant it’d be to provide a hint here, but it might be nice to warn the user if it’s detected that they’re trying to access a path method in a specific way when the model isn’t pluralizable.

e.g. series_path(id: 1) wouldn’t trigger the hint, but series_path with no arguments would cause Rails to print something like “You’re using series_path without an id. Series can’t be pluralized, maybe you meant series_index_path?”.

1 Like

I wouldn’t want to add this check to the generated method because it would incur a runtime cost. But I wonder if we could teach the “did you mean” gem about this case since it handles errors via exceptions.

4 Likes

@tenderlove A lot of the WTFs I’ve been seeing seem like they’re the kinds of things DidYouMean could help with.

1 Like

I made a POC here. I think we can make the code a bit smarter than what I’ve posted but I wanted to get something up that we could all look at! :smile:

3 Likes

This is great! Thank you for creating a PoC :smiley:

I cleaned up the PoC and merged it in, so I think we can call this complete? I’ll say this PR was the solution. I feel like many custom exceptions in Rails should have DYM support, but I think we should start opening tickets for each of them on the main GitHub issues page. I also don’t know which exceptions in particular should have DYM support, and I need help finding them.

6 Likes

This is great! :partying_face:

Probably very hard to do (if not impossible), but DYM support for instance variable typo’s in views would really help beginners…

I’ve created two PR’s for adding DYM for two more errors, inspired by your PoC.

There are still lot’s of NotFound or misspelled type errors… For example:

3 Likes

Awesome, thank you so much. I’m sure these will be a huge help!

2 Likes