controller_path(object) 返回 website.com/controller.1

发布于 2024-10-19 19:11:08 字数 1060 浏览 5 评论 0原文

我在 Rails 应用程序中为现有的脚手架模型手动创建了一个新视图,因为我认为这就是重点,我可以为同一模型创建多个视图。一切似乎都运行良好,除了当我尝试 link_to 视图中的某个项目时,Rails 给了我错误的 URL。

  • routes.rb 具有正确的“资源:控制器”
  • controller_controller.rb 存在且工作正常
  • 。views/controller/show.html.erb 存在且工作正常
  • link_to“controller”,controller_path # 工作正常

link_to“controller”,controller_path(object) #链接到控制器,但将 id 添加为“.id”而不是“/id”

有什么想法吗?

耙式路线输出

pups     GET /pups(.:format)          {:action=>"index", :controller=>"pups"} 
         POST /pups(.:format)         {:action=>"create", :controller=>"pups"} 
new_pup  GET /pups/new(.:format)      {:action=>"new", :controller=>"pups"} 
edit_pup GET /pups/:id/edit(.:format) {:action=>"edit", :controller=>"pups"}
pup      GET /pups/:id(.:format)      {:action=>"show", :controller=>"pups"} 
         PUT /pups/:id(.:format)      {:action=>"update", :controller=>"pups"} 
         DELETE /pups/:id(.:format)   {:action=>"destroy", :controller=>"pups"}

I have manually created a new View for an existing scaffolded Model in my Rails app because, well I thought that was the point, I can create multiple views for the same Model. Everything seems to function fine except that when I try to link_to an item in the view then Rails gives me the wrong URL.

  • routes.rb has the proper "resources :controller"
  • controller_controller.rb exists and works fine
  • views/controller/show.html.erb exists and works fine
  • link_to "controller", controller_path #works fine

link_to "controller", controller_path(object) # links to controller but adds the id as ".id" instead of "/id"

Any ideas why?

rake routes output

pups     GET /pups(.:format)          {:action=>"index", :controller=>"pups"} 
         POST /pups(.:format)         {:action=>"create", :controller=>"pups"} 
new_pup  GET /pups/new(.:format)      {:action=>"new", :controller=>"pups"} 
edit_pup GET /pups/:id/edit(.:format) {:action=>"edit", :controller=>"pups"}
pup      GET /pups/:id(.:format)      {:action=>"show", :controller=>"pups"} 
         PUT /pups/:id(.:format)      {:action=>"update", :controller=>"pups"} 
         DELETE /pups/:id(.:format)   {:action=>"destroy", :controller=>"pups"}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

—━☆沉默づ 2024-10-26 19:11:08

确保您的 link_to 使用的是 pup_path(object) 而不是 pups_path(object) (复数)。

Make sure your link_to is using pup_path(object) and not pups_path(object) (plural).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文