user1157352
user1157352

Reputation: 149

Nested Routing for Single Table Inheritance model rails 3.1

I created a Single table inheritance model in my model file and am having difficulty with the routing. When I use :as in my resource, it renames my named path.

Model file:

class Account < ActiveRecord::Base
    belongs_to :user
end
class AdvertiserAccount < Account
end
class PublisherAccount < Account
end

Routes.rb

resources :advertiser_accounts, :as => "accounts" do
    resources :campaigns
end

I used :as in my routes because it is a single table inheritance and I want to pass the account_id and not the advertiser_account_id. My link is http://127.0.0.1:3000/advertiser_accounts/1/campaigns

/advertiser_accounts/:account_id/campaigns/:id(.:format)

However, using :as renames my named path from advertiser_account_campaigns to account_campaigns. My route looks like

account_campaigns GET /advertiser_accounts/:account_id/campaigns(.:format) campaigns#index

So when I create a new item using form_for, I would get "undefined method `advertiser_account_campaigns_path'"

Edited: current hacked solution

A hack around way that I am using is to duplicate the code in the routes file. Anyone have suggestions?

resources :advertiser_accounts, :as => "accounts" do
    resources :campaigns
end
resources :advertiser_accounts do
    resources :campaigns
end

Upvotes: 2

Views: 765

Answers (1)

Berggeit
Berggeit

Reputation: 253

If you run "rake routes" with your setup you'll see this:

   account_campaigns  GET        /advertiser_accounts/:account_id/campaigns(.:format)          campaigns#index
                      POST       /advertiser_accounts/:account_id/campaigns(.:format)          campaigns#create
 new_account_campaign GET        /advertiser_accounts/:account_id/campaigns/new(.:format)      campaigns#new
edit_account_campaign GET        /advertiser_accounts/:account_id/campaigns/:id/edit(.:format) campaigns#edit
     account_campaign GET        /advertiser_accounts/:account_id/campaigns/:id(.:format)      campaigns#show
                      PUT        /advertiser_accounts/:account_id/campaigns/:id(.:format)      campaigns#update
                      DELETE     /advertiser_accounts/:account_id/campaigns/:id(.:format)      campaigns#destroy
             accounts GET        /advertiser_accounts(.:format)                                advertiser_accounts#index
                      POST       /advertiser_accounts(.:format)                                advertiser_accounts#create
          new_account GET        /advertiser_accounts/new(.:format)                            advertiser_accounts#new
         edit_account GET        /advertiser_accounts/:id/edit(.:format)                       advertiser_accounts#edit
              account GET        /advertiser_accounts/:id(.:format)                            advertiser_accounts#show
                      PUT        /advertiser_accounts/:id(.:format)                            advertiser_accounts#update
                      DELETE     /advertiser_accounts/:id(.:format)                            advertiser_accounts#destroy

So you should use "account_campaingns_path" in this setup, the ":as" actually changes the calls in the code not the paths in the url. If you want to change the paths you should use ":path =>" rather than ":as =>".

The Rails guide on routing also shows some examples with ":as" and ":path" and the resulting paths and helpers, you'll need to search a bit because think they only use in in examples explaining other cases.

Edit: rereading your question, I think you may also want to look at member routes, I'm not sure if that's what you want to mean with it being a single inheritance and not wanting to pass the advertiser_account's ':account_id'?

Upvotes: 0

Related Questions