實際上,我的工作就需要練習鏈接到許多子類的應用程序,並且子類別是一個父類的孩子。的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'
任何人都知道我在哪裏錯了?謝謝:)
這可能是值得集中一點關於錯誤的一些細節。例如,他們的堆棧跟蹤是什麼?另外你的表單代碼似乎有點困惑,例如:'對於運行在Subcategory.find(:all)'這是一個練習還是一個子類? – Shadwell
你是對的,我編輯了錯誤信息,並且我還修改了check_box_tag的內容。我看不出錯誤來自哪裏,你能幫忙嗎? –
偏離主題,但在一年的時間裏,當你意識到你拼錯了「運動」 - 現在我會修改它,這真的會讓你惱火。 – snowangel