0

I have a resource that inherits its table and controller from its parent. Its parent also has custom routing that I want to pass to it, though I'm not sure how (or if it's even possible). The routes currently look like this:

resources :publications do
  resources :editions, :controller => :publications
  collection do
    get :autocomplete, :claim, :current_users_publications, :lightbox, :lookup
    post :review
  end
  member do
    get :audit, :reassign_prompt
    post :approve, :audit_vote
    put :reassign
  end
end

With the current setup, the editions model does not have access to custom methods like "audit" or "autocomplete." Is it possible to do something like ":routes => :publications"?

nullnullnull
  • 8,039
  • 12
  • 55
  • 107

1 Answers1

1

Check out Routing Concerns

# Define the concern
concern :somethingable do
  collection do
    get :autocomplete, :claim, :current_users_publications, :lightbox, :lookup
    post :review
  end
  member do
    get :audit, :reassign_prompt
    post :approve, :audit_vote
    put :reassign
  end
end

# And now your routing
resources :publications, concerns: :somethingable do
  resources :editions, controller: :publications, concerns: :somethingable
end

I'm sure you can think of a better term than :somethingable to describe the common behavior

Update

Because the above is for rails master branch, there are a couple alternatives you can utilize

  1. There is a gem that abstracts this behavior for use in Rails 3.2+
  2. Instead of using concern, just create a method in your routing file.

    def somethingable
      collection do
        get :autocomplete, :claim, :current_users_publications, :lightbox, :lookup
        post :review
      end
      member do
        get :audit, :reassign_prompt
        post :approve, :audit_vote
        put :reassign
      end
    end
    

    then your routes might look like

    resources :publications do
      somethingable
    
      resources :editions, controller: :publications do
        somethingable
      end
    end
    
deefour
  • 34,974
  • 7
  • 97
  • 90
  • This isn't working for me, though neither am I using Edge Rails. Perhaps I should look into updating. Either way, it's great to know this is there. – nullnullnull Nov 27 '12 at 20:27
  • I hope my update bumps you in the right direction for non-`master` branch rails. – deefour Nov 27 '12 at 20:35