I'm actually working on an app that needs to link an exercise to many subcategories, and the subcategories are children of a parent category.
This is my code:
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 & has_many :subcategories
class Category < ActiveRecord::Base
attr_accessible :name
has_many :exercices, :through => :subcategories
has_many :subcategories
end
subcategory.rb belongs_to :category & 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 view I want to : Assign the subcategories for the exercise
<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>
Subcategories _form view I want to : assign a category for the subcategories
<li class="field">
<%= f.label :category %><br />
<%= f.collection_select(:category_id, @categories, :id, :name, :include_blank => "Please select") %>
</li>
Whatever the form, I got an error message :
=> subcategories/new :
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/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'
Anyone knows where I am wrong ? Thanks :)