controller_path(object) 返回 website.com/controller.1
我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您的 link_to 使用的是
pup_path(object)
而不是pups_path(object)
(复数)。Make sure your link_to is using
pup_path(object)
and notpups_path(object)
(plural).