路径助手用点代替斜杠生成路径

在我的路线中 rb 我有以下几点:

resources :message_threads

当我呼唤:

message_threads_path(1)

我得到了:

/message_threads.1

为什么会这样? 我的其他资源工作得很好。难道我不能正确地复数这个还是什么?

21202 次浏览

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

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)

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).