使用 Rails url_for帮助程序生成的意外嵌套路由



我正在使用 Rails 5,我的路线看起来像这样:

Rails.application.routes.draw do
    namespace :admin do
      namespace :moderation do
        resources :templates
      end
      resources :users, except: [:new, :create] do
        namespace :moderation do
          resources :messages, only: [:index, :show, :new, :create, :update, :edit] do
            resources :message_replies, only: [:create, :edit, :update]
          end
          resources :templates
        end
      end
      root "home#index"
    end
end

我有models/admin/user.rb

module Admin
  class User < ::User
  end
end

models/admin/moderation/template.rb

module Admin
  module Moderation
    class Template < ::Moderation::Template
    end
  end
end

现在,当我尝试使用助手生成正确的路由时url_for

2.4.1 (main):0 > admin_user = Admin::User.first
2.4.1 (main):0 > admin_moderation_template = Admin::Moderation::Template.first
2.4.1 (main):0 > url_for([admin_user, admin_moderation_template])
NoMethodError: undefined method `admin_user_admin_moderation_template_url' for main:Object

我没有得到admin_user_moderation_template_url我得到了admin_user_admin_moderation_template_url.在这种情况下,我应该如何生成正确的路线有什么帮助吗?

我想知道您是否可以使用此xxx_url()助手来解决。在您的情况下,它将是:

admin_user_moderation_template_url( user_id: admin_user.id, id: admin_moderation_template.id ) 

.

编辑

不幸的是,我无法尝试它,因为我必须构建您正在处理的应用程序。但如本答案所述,您可以使用符号来创建admin_user_moderation_template_url .

尝试运行以下行并告诉我它是否有效(我不确定user_id处于正确的位置,但它可能有效(。

url_for( [:admin, :user, :moderation, :template, user_id: admin_user.id, id: admin_moderation_template.id ])

您还需要用户和模板之间的关联才能链接它们。

最新更新