【问题标题】:Rails: uninitialized constant ControllerRails:未初始化的常量控制器
【发布时间】:2018-03-27 00:13:14
【问题描述】:

这是我的设置:

路线:

resources :apps, except: [:index, :new], path: 'a' do
    resources :platforms, only: [:update]
end

控制器:

class Apps::PlatformsController < ApplicationController

    before_action :authenticate_user!
    before_action :set_platform

    # PATCH/PUT /apps/1/platforms/1
    # PATCH/PUT /apps/1/platforms/1.json
    def update
        # do stuff ...
    end

    private

    # Use callbacks to share common setup or constraints between actions.
    def set_platform
        @platform = App::Platform.find params[:id]
    end

end

查看:

= link_to 'Update', app_platform_url(platform.app.slug, platform.id), method: :put

点击链接时出现以下错误:

未初始化的常量 PlatformsController


为什么 Rails 会寻找 PlatformsController 而不是 Apps::PlatformsController

【问题讨论】:

    标签: ruby-on-rails ruby controller routes uninitialized-constant


    【解决方案1】:

    据我所知,只有 namespaces 和 scopes 需要嵌套模块。

    【讨论】:

    • 你知道比命名每个嵌套路由更好的解决方案吗?因为这会创建像app_apps_platform_url 这样的丑陋的 url 助手。
    • 你可以用一个块来命名所有的路由
    • 如果路由名称有问题,或者使用 as 选项重命名您的路由
    猜你喜欢
    • 2013-11-18
    • 1970-01-01
    • 2015-10-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    相关资源
    最近更新 更多