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

Nic*_*5a1 4 ruby-on-rails ruby-on-rails-3

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

我在视图中有上面的代码,但是在单击链接时出现以下错误: No route matches {:action=>"show", :controller=>"equipment"}

我的路线文件包含:

resources :equipment

resources :workouts

match ':controller(/:action(/:id))(.:format)'
Run Code Online (Sandbox Code Playgroud)

为什么要尝试访问show动作?

以下是我的路线中的条目:

   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
Run Code Online (Sandbox Code Playgroud)

edr*_*lph 6

此问题之前出现过,与rails scaffolding如何为名为"设备"的模型生成new.html.erb文件有关,这些模型既有单数也有复数.

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

建议通常是"如果可以的话,避免像这样的模型名称",或者它涉及到使用config/initializers/inflections.rb文件来强制复制版本的模型名称.当然,你最终会得到一个非常奇怪的模型引用的应用程序:"设备"不是很好用(以后有人会"修复"它,再搞乱一下).

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

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

和link_to:

<%= link_to 'Back', equipment_index_path %>