'btn btn-primary' %> 我在 View 中有上述代码,但单击链接时出现-6ren">
gpt4 book ai didi

ruby-on-rails - 当模型单数和复数名称相同时(例如设备、物种),rails 链接路径和路由错误

转载 作者:行者123 更新时间:2023-12-03 00:29:13 25 4
gpt4 key购买 nike

<%= link_to t('.new', :default => t("helpers.links.new")), new_equipment_path, :class => 'btn btn-primary' %>

我在 View 中有上述代码,但单击链接时出现以下错误:没有路线匹配 {:action=>"show", :controller=>"equipment"}

我的路线文件包含:

resources :equipment

resources :workouts

match ':controller(/:action(/:id))(.:format)'

为什么它试图访问显示操作?

以下是我的 route 的条目:

   equipment_index GET        /equipment(.:format)                   equipment#index
POST /equipment(.:format) equipment#create
new_equipment GET /equipment/new(.:format) equipment#new
edit_equipment GET /equipment/:id/edit(.:format) equipment#edit
equipment GET /equipment/:id(.:format) equipment#show
PUT /equipment/:id(.:format) equipment#update
DELETE /equipment/:id(.:format) equipment#destroy

最佳答案

此问题有 cropped up before 并且与 Rails 脚手架如何为名称为“equipment”(单数和复数)的模型生成 new.html.erb 文件有关。

如果您检查 form_for在 new.html.erb 文件中您将看到 equipment_path在底部的 link_to 中。对于这些具有单数==复数名称的模型,指的是实际上用于 show 的路线。操作,因此您的错误消息。

建议通常是“如果可以的话,避免使用这样的模型名称”,或者涉及对 config/initializers/inflections.rb 文件进行一些修改,以强制使用复数版本的模型名称。当然,最终你会得到一个应用程序,其中对模型的引用听起来非常奇怪:“设备”不太好用(后来有人会“修复”它,再次把事情搞砸)。

为了保持模型名称在语法上正确,您需要修复 form_for 即:

<% form_for(@equipment, :url=> {:action=>'create'}) do |f| %>

和链接:

<%= link_to 'Back', equipment_index_path %>

关于ruby-on-rails - 当模型单数和复数名称相同时(例如设备、物种),rails 链接路径和路由错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11371792/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com