【问题标题】:uninitialized constant -- model within controller未初始化的常量——控制器内的模型
【发布时间】:2023-03-15 13:22:01
【问题描述】:

我有一个应用程序,其中一个俱乐部有_many 个位置。俱乐部及其位置只能在管理员命名空间内进行编辑。

我正在尝试将球杆预加载到控制器中,以便所有操作仅处理该球杆。

路由是嵌套的;但是,在位置控制器中,它找不到 Club 模型。我做错了什么?

routes.rb

namespace :admin do
  resources :clubs do
    resources :locations
  end
end

club.rb

class Club < ActiveRecord::Base
  belongs_to :membership
  has_many :users
  has_many :locations
  #accepts_nested_attributes_for :locations
end

admin/locations_controller.rb

class Admin::LocationsController < ApplicationController
  before_filter :load_club

  protected 

  def load_club
    @club = Club.find(params[:club_id])
  end
end

另外,最后:我的路线有什么问题,它没有在 admin/clubs/locations 中寻找位置控制器?我不确定这是否是问题的一部分。

来自 rake 路线

    admin_club_locations POST   /admin/clubs/:club_id/locations(.:format)          admin/locations#create
 new_admin_club_location GET    /admin/clubs/:club_id/locations/new(.:format)      admin/locations#new
edit_admin_club_location GET    /admin/clubs/:club_id/locations/:id/edit(.:format) admin/locations#edit
     admin_club_location PUT    /admin/clubs/:club_id/locations/:id(.:format)      admin/locations#update
                         DELETE /admin/clubs/:club_id/locations/:id(.:format)      admin/locations#destroy

【问题讨论】:

    标签: ruby-on-rails routes actioncontroller


    【解决方案1】:

    它可能会在当前 Admin 命名空间中查找 Club 模型。你可以试试:

    def load_club
      @club = ::Club.find(params[:club_id])
    end
    

    【讨论】:

    • 是的,这是模型的命名空间问题。谢谢!我在创建问题后立即想通了。 >.>
    猜你喜欢
    • 1970-01-01
    • 2014-08-01
    • 2018-03-27
    • 2013-11-18
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多