Path helpers generate paths with dots instead of slashes

Ruby on-RailsRuby on-Rails-3

Ruby on-Rails Problem Overview


In my routes.rb I have the following:

resources :message_threads

When I call:

message_threads_path(1)

I get:

/message_threads.1

Why is this? My other resources work fine. Am I not pluralizing this correctly or something?

Ruby on-Rails Solutions


Solution 1 - Ruby on-Rails

Yes, this is a pluralization error.

By passing the ID 1, I assume that you wish to display a single record.

So you need to use the singular 'message_thread':

message_thread_path(1)

Which will yield:

http://localhost:3000/message_threads/1

Solution 2 - Ruby on-Rails

Sometimes this also is when you don't provide an :as parameter in your route:

delete "delete/:id" => "home#delete"

Changed to:

delete "delete/:id" => "home#delete", as: :delete

(ignore the odd example, just happened to be something we just ran into for an internal app we're building)

Solution 3 - Ruby on-Rails

Other folks that land here might be in this situation:

If you have a singular resource declared in your routes.rb:

resource :map

You don't need to pass an object to map_path. Attempting to call map_path(map) will result in similar behavior (i.e. a URL like map.12).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionghemptonView Question on Stackoverflow
Solution 1 - Ruby on-RailsScottView Answer on Stackoverflow
Solution 2 - Ruby on-RailsJamon HolmgrenView Answer on Stackoverflow
Solution 3 - Ruby on-RailstechpeaceView Answer on Stackoverflow