3

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
0

1 Answer 1

9

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
1
  • 2
    Yes, it was a namespacing issue with the model. Thanks! I figured it out immediately after I created the question. >.>
    – Steph Rose
    Jun 24, 2013 at 15:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.