2013-08-28 73 views
-1

實際上,我的工作就需要練習鏈接到許多子類的應用程序,並且子類別是一個父類的孩子。的Rails 3.2類別和子類別

這是我的代碼:

Exercice.rb belongs_to的:子類別

class Exercice < ActiveRecord::Base 
attr_accessible :title, :description, :subcategory_id 

validates :title, :presence => true 

has_many :programmeexercices 
has_many :programmes, :through => :programmeexercices 
belongs_to :subcategory 

end 

category.rb的has_many:exercices通過:子類別&的has_many:小類

class Category < ActiveRecord::Base 
attr_accessible :name 

has_many :exercices, :through => :subcategories 
has_many :subcategories 
end 

subcategory.rb belongs_to的:類&的has_many:exercices

class Subcategory < ActiveRecord::Base 
attr_accessible :name, :category_id 

belongs_to :category 
has_many :exercices 
end 

Exercices_controller

def new 
    @exercice = Exercice.new 
    @subcategories = Subcategory.all 
    end 

    def edit 
    @exercice = Exercice.find(params[:id]) 
    @subcategories = Subcategory.all 
    end 

    def create 
    @exercice = Exercice.new(params[:exercice]) 
    params[:subcategories][:id].each do |subcategory| 
     if !subcategory.empty? 
     @exercice.subcategory.build(:subcategory_id => subcategory) 
     end 
    end 

Category_controller

def new 
    @category = Category.new 
    end 

    def edit 
    @category = Category.find(params[:id]) 
    end 

def create 
    @category = Category.new(params[:category]) 
    end 

Subcategory_controller

def new 
    @subcategory = Subcategory.new 
    @categories = Category.all 
    end 

def edit 
    @subcategory = Subcategory.find(params[:id]) 
    @categories = Category.all 
    end 

    def create 
    @subcategory = Subcategory.new(params[:subcategory]) 

    params[:categories][:id].each do |category| 
     if !category.empty? 
     @subcategory.category.build(:category_id => category) 
     end 
    end 

Exercice _form視圖我想:分配的子類別進行演練

 <li id="p-select"> 
     <% for subcategory in Subcategory.find(:all) %> 
     <div class="row"> 
      <section class="twelve columns"> 
      <article class="valign row"> 
       <div> 
       <label class="checkbox"> 
        <%= check_box_tag "checkboxInput", subcategory.id, @exercice.subcategories.include?(subcategory) %>          
       </label> 
       </div> 
       <div> 
       <h3><%= exercice.title %></h3> 
       </div>      
      </article> 
      <% end %> 
      </section> 
     </div> 
     </li> 
    </li> 

小類_form視圖我想:指定類別的子類別

<li class="field"> 
     <%= f.label :category %><br /> 
     <%= f.collection_select(:category_id, @categories, :id, :name, :include_blank => "Please select") %> 
    </li> 

不管是什麼形式,我得到一個錯誤信息:

=>子類/新:

NoMethodError in Subcategories#new 
undefined method `category_id' for #<Subcategory:0x4f676b8> 

app/views/subcategories/_form.html.erb:16:in `block in _app_views_subcategories__form_html_erb___180268787_42055560' 
app/views/subcategories/_form.html.erb:1:in `_app_views_subcategories__form_html_erb___180268787_42055560' 
app/views/subcategories/new.html.erb:3:in `_app_views_subcategories_new_html_erb___1058729489_29352300' 
app/controllers/subcategories_controller.rb:30:in `new' 

=> exercice /新:

NoMethodError in Exercices#new 
undefined method `subcategories' for #<Exercice:0x4c992c0> 

    app/views/exercices/_form.html.erb:31:in `block (2 levels) in _app_views_exercices__form_html_erb__725096104_41604048' 
app/views/exercices/_form.html.erb:25:in `each' 
app/views/exercices/_form.html.erb:25:in `block in _app_views_exercices__form_html_erb__725096104_41604048' 
app/views/exercices/_form.html.erb:2:in `_app_views_exercices__form_html_erb__725096104_41604048' 
app/views/exercices/new.html.erb:3:in `_app_views_exercices_new_html_erb___924742088_42063192' 
app/controllers/exercices_controller.rb:31:in `new' 

任何人都知道我在哪裏錯了?謝謝:)

+1

這可能是值得集中一點關於錯誤的一些細節。例如,他們的堆棧跟蹤是什麼?另外你的表單代碼似乎有點困惑,例如:'對於運行在Subcategory.find(:all)'這是一個練習還是一個子類? – Shadwell

+0

你是對的,我編輯了錯誤信息,並且我還修改了check_box_tag的內容。我看不出錯誤來自哪裏,你能幫忙嗎? –

+0

偏離主題,但在一年的時間裏,當你意識到你拼錯了「運動」 - 現在我會修改它,這真的會讓你惱火。 – snowangel

回答

0

說實話,我認爲你需要回去返工大量的代碼。

您的電話號碼爲exercices.subcategories,導致您的第二個錯誤,因爲Exercisebelongs_to :subcategory因此每次練習只能有一個子類別。

你遍歷所有子類別爲每個提示你需要的是一個鍛鍊,有許多子類的運動形式提供相應的複選框。

您已將您的複選框的名稱加稅checkBoxInput,然後您希望在控制器中使用參數subcategories[]

您還輸出每個子類別的練習題目。

因此,在嘗試解決特定錯誤之前,我會向後採取幾個步驟並整理代碼的許多邏輯。

0

好吧,我明白了!

我想要的是: 練習有多個子類別和一個類別。 子類別只有一個類別。 一個類別有多個子類別。

****class Exercice < ActiveRecord::Base**** 
    attr_accessible :title, :subcategory_id 

    has_many :subcategories 
    has_one :category, :through => :subcategories 


****class Category < ActiveRecord::Base***** 
    attr_accessible :name 

    has_many :subcategories 
    has_many :exercices, :through => :subcategories 


***class Subcategory < ActiveRecord::Base**** 
    attr_accessible :name, :category_id 

    belongs_to :category 
    has_many :exercices 

是嗎?