我实际上正在开发一个需要将练习链接到多个子类别的应用程序,子类别是父类别的子类。
这是我的代码:
Exercice.rb belongs_to:subcategory
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 through:subcategories&amp; has_many:子类别
class Category < ActiveRecord::Base
attr_accessible :name
has_many :exercices, :through => :subcategories
has_many :subcategories
end
subcategory.rb belongs_to:category&amp; 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
练习_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>
子类别_形成视图我想:为子类别指定一个类别
<li class="field">
<%= f.label :category %><br />
<%= f.collection_select(:category_id, @categories, :id, :name, :include_blank => "Please select") %>
</li>
无论表格是什么,我都收到错误消息:
=&GT;子类别/新:
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'
=&GT; exercice / new:
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'
谁知道我哪里错了?谢谢:))
答案 0 :(得分:0)
老实说,我认为你需要回去修改很多代码。
您的表单中有exercices.subcategories
来电,导致您的第二次错误,因为Exercise
belongs_to :subcategory
因此每次练习只能有一个子类别。
您循环遍历所有子类别,为练习表单上的每个子类别提供一个复选框,表示您实际上希望练习具有多个子类别。
您已将复选框的税号命名为checkBoxInput
,然后您希望控制器中有参数subcategories[]
。
您还要输出每个子类别的练习题。
因此,在尝试修复特定错误之前,我会向后退几步并整理代码的大部分逻辑。
答案 1 :(得分: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
那是对的吗?