2

I am using single table inheritance in ROR3 so I created these models:

class Promotion < ActiveRecord::Base

...

end

class CreditPromotion < Promotion
end

class DebitPromotion < Promotion
end

Then I created a general form in a partial view

<%= form_for([:admin, @promotion]) do |f| %>
      #Many fields for promotion
<% end %>

In new action this form work very well. However in edit action throw this error message:

NoMethodError in Admin/promotions#edit

Showing /home/mperez/Proyectos/Habitue/commerce/commerce/app/views/admin/promotions/_form.html.erb where line #48 raised:

undefined method `admin_debit_promotion_path' for #<#<Class:0x7fcf57ada178>:0x7fcf57ad6820>

Extracted source (around line #48):

45:     });
46: </script>
47: 
48: <%= form_for([:admin, @promotion]) do |f| %>
49:     <% if @promotion.errors.any? %>
50:         <div id="error_explanation">
51:           <h2><%= pluralize(@promotion.errors.count, "error") %> prohibited this promotion from being saved:</h2>

Update: ADD ROUTES

Rails3::Application.routes.draw do

  # Administration
  namespace :admin do
    match 'dashboard/update_cities' => 'dashboard#update_cities', :as => :update_cities
    match 'dashboard/update_departments' => 'dashboard#update_departments', :as => :update_departments
    match 'dashboard/update_towns' => 'dashboard#update_towns', :as => :update_towns
    match 'promotions/unlimmited_quota' => 'promotions#unlimmited_quota', :as => :unlimmited_quota
    match 'promotions/unlimmited_limit_per_user' => 'promotions#unlimmited_limit_per_user', :as => :unlimmited_limit_per_user

    root :to => 'dashboard#index'

    resources :settings
    match '/settings/update_settings' => 'settings#update_settings',  :requirements => { :method => :post }
    resources :announcements
    resources :commits
    resources :sellers
    resources :commerces
    resources :commerce_visits
    resources :promotions do
      collection do
        post :load_promotion_fields
      end
    end

    #resources :debit_promotions, :controller => :promotions
    #resources :credit_promotions, :controller => :promotions
    #resources :gift_promotions, :controller => :promotions

    resources :store_points

    match '/users/search' => 'users#search',  :requirements => { :method => :get }

    resources :users do 
      member do 
        put :suspend
        put :unsuspend
        put :activate
        delete :purge
        put :reset_password
        get :set_user_login
        get :set_user_email        
      end
      collection do
        get :pending
        get :active
        get :suspended
        get :deleted
      end
    end
  end
end

Which could be the problem?

I understand a problem with the routes. However I don´t want change it.

How could use single table inheritance with a general form?

Thanks in advance

maxiperez
  • 1,470
  • 2
  • 20
  • 40

2 Answers2

3

When you come to edit your Promotion it has already been instantiated as a DebitPromotion.

form_for([:admin, @promotion])

tries to go to the nonexistent admin_debit_promotion_path(@promotion)

So override it to go to the correct route and controller like this:

form_for([:admin, @promotion], :url => admin_promotion_path(@promotion))
aimee
  • 48
  • 4
  • OK. thank you very much. The route problem was solved. My new problem are nested attributes in this form. Then i created a new post. – maxiperez Sep 22 '11 at 19:50
  • This article gives a number of useful tips for dealing with Single Table Inheritance in Rails. http://code.alexreisner.com/articles/single-table-inheritance-in-rails.html – aimee Sep 27 '11 at 23:00
  • The Alex Reisner article can now be found here: http://www.alexreisner.com/code/single-table-inheritance-in-rails – tirdadc Apr 12 '14 at 12:33
0

Correct form to solve this problem:

form_for([:admin, @promotion],
         as: :promotion,
         url: admin_promotion_path(@promotion) ) do |f|
Iazel
  • 2,296
  • 19
  • 23