我使用的是Rails5,我的路径类似于:
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。在这种情况下,如何生成正确的路由有什么帮助吗?
发布于 2017-05-24 17:20:10
我想知道你是否可以使用这个xxx_url()助手来解决这个问题。在您的情况下,它将是:
admin_user_moderation_template_url( user_id: admin_user.id, id: admin_moderation_template.id ) 。
编辑
不幸的是,我不能尝试它,因为我将不得不建立你正在工作的应用程序。但正如this answer中所述,您可以使用符号来创建admin_user_moderation_template_url。
尝试运行以下代码行,并告诉我它是否可以工作(我不确定user_id是否处于正确的位置,但它可能会工作)。
url_for( [:admin, :user, :moderation, :template, user_id: admin_user.id, id: admin_moderation_template.id ])您还需要用户和模板之间的关联才能将它们链接起来。
https://stackoverflow.com/questions/44152570
复制相似问题