简体   繁体   English

Rails无法确定词根的模型的路由问题

[英]Routing Issue for Model that Rails Can't Determine Word Roots

I have a model named Faq and it looks like Rails is having a hard time with the dynamic paths generated from resources :faq . 我有一个名为Faq的模型,看起来Rails很难处理从resources :faq生成的动态路径。

Here is what rake routes puts out. 这是rake routes输出。

   admin_faq_index GET    /admin/faq(.:format)                                   admin/faq#index
                   POST   /admin/faq(.:format)                                   admin/faq#create
     new_admin_faq GET    /admin/faq/new(.:format)                               admin/faq#new
    edit_admin_faq GET    /admin/faq/:id/edit(.:format)                          admin/faq#edit
         admin_faq GET    /admin/faq/:id(.:format)                               admin/faq#show
                   PUT    /admin/faq/:id(.:format)                               admin/faq#update
                   DELETE /admin/faq/:id(.:format)                               admin/faq#destroy

The problem is when I use form_for like so: 问题是当我像这样使用form_for时:

<%= form_for([:admin, @faq]) do |f| %>

I get this error: 我收到此错误:

undefined method `admin_faqs_path' for #<#<Class:0x007fdda4627a58>:0x007fdda41a5098>

Try changing routes.rb to 尝试将route.rb更改为

resources :faqs

Then verify that @faq is not nil. 然后验证@faq不是零。

Based on the fact that you're using a form, I'm guessing your FAQ is not a singular resource. 基于您使用表单的事实,我猜您的FAQ不是唯一的资源。

You just have to add an excpetion in config/initializers/inflector.rb 您只需要在config/initializers/inflector.rb添加一个config/initializers/inflector.rb

like this one 像这个

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable %w(faq)
end

It add an exception to pluralize and inflector rules of rails 它为铁轨的复数和变形规则添加了例外

As you have mentioned in routes resources :faq , it has not generate the routes admin_faqs_path but in the form_for by default it is searching for the admin_faqs_path . 如您在路由resources :faq所提到的,它没有生成路由admin_faqs_path但是默认情况下,它在form_for正在搜索admin_faqs_path So we need to override it. 所以我们需要重写它。 Please change the code to 请更改代码为

<%= form_for([:admin, @faq], :url => admin_faq_index_path, :method => :post) do |f| %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM