当模型单数和复数名称相同(例如设备、物种)时,rails 链接路径和路由错误 [英] rails link path and routing error when model singular and plural name are the same (e.g. equipment, species)

查看:55
本文介绍了当模型单数和复数名称相同(例如设备、物种)时,rails 链接路径和路由错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我的路由文件包含:

资源:设备资源:锻炼匹配 ':controller(/:action(/:id))(.:format)'

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

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

 equipment_index GET/equipment(.:format) devices#indexPOST/equipment(.:format) 设备#createnew_equipment GET/equipment/new(.:format) 设备#newedit_equipment GET/equipment/:id/edit(.:format) 设备#edit设备 GET/equipment/:id(.:format) 设备#showPUT/equipment/:id(.:format) 设备#update删除/equipment/:id(.:format) 设备#destroy

解决方案

这个问题有之前出现过,与 rails scaffolding 如何为具有单数和复数形式的equipment"等名称的模型生成 new.html.erb 文件有关.>

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

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

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

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

和链接到:

<%= link_to 'Back', equipment_index_path %>

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

I have the above code in a view, but am getting the following error when clicking the link: No route matches {:action=>"show", :controller=>"equipment"}

My routes file contains:

resources :equipment

resources :workouts

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

Why is it trying to access the show action?

Here are the entries from my routes:

   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

解决方案

This issue has cropped up before and is related to how rails scaffolding generates the new.html.erb file for models that have names like 'equipment' which are both singular and plural.

If you inspect the form_for in the new.html.erb file you'll see equipment_path in the link_to at the bottom. For these models with singular==plural names that refers to a route that is actually for the show action, hence your error message.

The advice is often along the lines of 'avoid model names like this if you can' or it involves a bit of messing around with the config/initializers/inflections.rb file to force a plural version of the model name. Of course then you end up with an app with very odd sounding references to models: 'equipments' isn't very nice to work with (and someone later on will 'fix' it, messing things up again).

To keep the model name grammatically correct, you need to fix the form_for i.e.:

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

and the link_to:

<%= link_to 'Back', equipment_index_path %>

这篇关于当模型单数和复数名称相同(例如设备、物种)时,rails 链接路径和路由错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆