编辑表单中的单表继承问题

时间:2011-09-20 17:18:15

标签: ruby-on-rails-3 forms single-table-inheritance

我在ROR3中使用单表继承,所以我创建了这些模型:

class Promotion < ActiveRecord::Base

...

end

class CreditPromotion < Promotion
end

class DebitPromotion < Promotion
end

然后我在局部视图中创建了一般表格

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

在新的行动中,这种形式非常有效。但是在编辑操作中抛出此错误消息:

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>

更新:添加路线

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

哪个可能是问题?

我理解路线的问题。但是我不想改变它。

如何使用一般表格的单表继承?

提前致谢

2 个答案:

答案 0 :(得分:3)

当您开始编辑促销时,它已经被实例化为DebitPromotion。

form_for([:admin, @promotion])

尝试转到不存在的admin_debit_promotion_path(@promotion)

所以重写它以转到正确的路线和控制器,如下所示:

form_for([:admin, @promotion], :url => admin_promotion_path(@promotion))

答案 1 :(得分:0)

正确的表格来解决这个问题:

form_for([:admin, @promotion],
         as: :promotion,
         url: admin_promotion_path(@promotion) ) do |f|