2

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
user1157352
  • 149
  • 2
  • 10
  • Can you post your erb/form code as well? thanks – plainjimbo Apr 01 '12 at 00:30
  • 1
    Actually, this isn't just for the form code. I am not able to link to my nested path if I use :as. For instance, link_to code <%= link_to 'Show All Campaign', advertiser_account_campaigns_path(@advertiser_account) %> will return "undefined method `advertiser_account_campaigns_path'". This is because the named path for advertiser_account_campaigns_path got renamed to account_campaigns_path when I use :as=> "accounts". I want to be able to create a valid link like 127.0.0.1:3000/advertiser_accounts/10/campaigns without duplicating too much code in routes.rb – user1157352 Apr 01 '12 at 03:54
  • having the same problem, did you manage to find a solution yet? – pdu Feb 18 '13 at 21:34

1 Answers1

0

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'?

Berggeit
  • 253
  • 1
  • 2
  • 11