Sam Kong
Sam Kong

Reputation: 5840

Restful Rails Question

I have 2 models.

class Company < ActiveRecord::Base
  has_many :accounts, :class_name => "CompanyAccount"
end

class CompanyAccount < ActiveRecord::Base
  belongs_to :company
end

In routes.rb

resources :companies do
  resources :accounts
end

I have companies_controller.rb and accounts_controller.rb.

The following line doesn't work because @account's class is not Account but CompanyAccount.

= form_for [@company, @account] do

What's the best way to resolve this name mismatch?

Thanks.

Upvotes: 1

Views: 67

Answers (2)

RubyFanatic
RubyFanatic

Reputation: 2281

The problem is in your routes.rb file,

The code should still be

resources :companies do |c|
  c.resources :company_accounts
end

It does not matter what you name in your association has_many <association_name>, the resource name should always reflect the name of your actual model and not the ActiveRecord association. Hope it helps!

Upvotes: 1

tjeden
tjeden

Reputation: 2079

You can try to use:

= form_for @account, :as => :company_account, :url => company_account_path(@company, @account)

Upvotes: 0

Related Questions