I have an habtm problem, which I just can’t figure out for the life of
me. This is a continuation of another problem I had (on this thread:
Habtm and collection_select question - Rails - Ruby-Forum), which someone was kind
enough to help me with.)
To recap, I have tables Projects and Stages, with a habtm relationship.
I have a table Projects_Stages contains project_id, stages_id and an
additional date_done field (containing the date the stage of the project
was completed).
In addition to letting the user select the next stage of the project, I
want them to be able to select the date that stage was completed, so I
need a date select field on the page.
My models:
class Stages < AR
has_and_belongs_to_many :projects
end
class Projects < AR
has_and_belongs_to_many :stages
attr_accessor :newly_completed_stage_id
attr_accessor :stage_date_done
def before_save
stages.push_with_attributes(Stage.find(newly_completed_stage_id.to_i),
:date_done => stage_date_done) unless newly_completed_stage_id.blank?
end
end
My edit.rhtml page contains (as part of the form_tag):
<%= collection_select(:project, :newly_completed_stage_id,
Step.find(:all) - @project.stages, :id, :description, {:include_blank =>
true}) %>
<%= date_select (:project, :stage_date_done, :order => [:day, :month,
:year]) %>
The collection_select works just fine on its own. However, adding the
date_select to the edit.rhtml page as shown above, returns the following
error:
undefined method `klass’ for nil:NilClass
Trace shows parameters are being passed (some data omitted):
Parameters: {“commit”=>“Save”, “id”=>“1”,
“project”=>{“stage_date_done(3i)”=>“13”, “newly_completed_step_id”=>“5”,
“stage_date_done(1i)”=>“2005”, “stage_date_done(2i)”=>“12”}}
If I omit the date_select command in edit.rhtml and change my
before_save method to “:date_done => Date.today”, then everything works
properly. So the problem is with the date_select command or the
stage_date_done attribute. But I can’t find anything wrong with it, and
I can’t find any information on what the ‘klass’ method is. So I’m
stuck! Is this just an habtm limitation or am I missing something?
The only thing I can think of is that date_select returns an array (as
evidenced by the parameters passed), and that maybe I need to somehow
define stage_date_done as an array using attr_accessor? However, I can’t
find any way to do this (I have both the Programming Ruby and Agile
books).
Thanks!