Question

I have an application in which a club has_many locations. Clubs & their locations can only be edited within an admin namespace.

I am trying to pre-load the club into the controller so that all actions deal with that club only.

The routes are nested; however, in the locations controller, it does not find the Club model. What am I doing wrong?

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

Also, lastly: What is wrong with my routes that it is not looking for the locations controller in admin/clubs/locations? I'm not sure if that is part of the problem.

from rake routes

    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
Was it helpful?

Solution

It probably looks up the Club model within the current Admin namespace. You could try:

def load_club
  @club = ::Club.find(params[:club_id])
end
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top