加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails – 当模型单数和复数名称相同时(例如设备,物种),r

发布时间:2020-12-17 03:03:21 所属栏目:百科 来源:网络整理
导读:%= link_to t('.new',:default = t("helpers.links.new")),new_equipment_path,:class = 'btn btn-primary' % 我在视图中有上面的代码,但是在点击链接时出现以下错误:没有路由匹配{:action =“show”,:controller =“equipment”} 我的路线文件包含: res
<%= link_to t('.new',:default => t("helpers.links.new")),new_equipment_path,:class => 'btn btn-primary' %>

我在视图中有上面的代码,但是在点击链接时出现以下错误:没有路由匹配{:action =>“show”,:controller =>“equipment”}

我的路线文件包含:

resources :equipment

resources :workouts

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

为什么要尝试访问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

解决方法

这个问题有 cropped up before,与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 | %GT;

和link_to:

<%= link_to'Back',equipment_index_path%>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读