-1

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 :)

  • 1
    It is probably worth concentrating a little more on giving some details about the errors. What is the stacktrace for them for example? Also your form code seems a little confused, for example: `for exercice in Subcategory.find(:all)` is it an exercise or a subcategory? – Shadwell Aug 28 '13 at 17:34
  • You're right, I've edited the error messages, and I've also modified the content of the check_box_tag. I can't see where the errors come from, can you help ? – Leonardo Da Silva Aug 28 '13 at 19:00
  • Off topic, but it's really going to annoy you in a year's time when you realise you've spelled 'exercise' wrong -- I'd amend it now. – snowangel Aug 28 '13 at 21:03
  • But it's french !!! :) EDIT: Ive modified the wrong english words in muh text, im sorry about it :) – Leonardo Da Silva Aug 29 '13 at 01:20

2 Answers2

0

To be honest I think you need to go back and rework a lot of the code.

You've got a call to exercices.subcategories in your form which is causing your second error because Exercise belongs_to :subcategory so you can only have one subcategory per exercise.

You're looping over all subcategories to provide a check box for each on the exercise form which suggests you actually want an exercise to have many subcategories.

You've named your check box tax checkBoxInput and then you're expecting a parameter subcategories[] in your controller.

You're also outputting the exercise title for each subcategory.

So, I'd take a few steps backwards and tidy up a lot of the logic of the code before trying to fix specific errors.

Shadwell
  • 34,314
  • 14
  • 94
  • 99
0

Ok, I got it !

What I want is : An exercise has multiple subcategories and just one category. A subcategory has only one category. A category has multiple subcategories.

****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

That's right ?