The final hangup before this site goes to live beta and it is a big one

Ok everyone. I appreciate your time if you are looking at this, as
this site is being setup to assist veterans who are having trouble
with the health care system through the VA. And as I said, we are
pretty close at this point to going live beta so if you can help me
over this hump it would be extremely appreciated.

We have a form setup that asks the user to select a date when
treatment began and then when treatment ended. After that they fill
out a free text field explaining what kind of problems they
experienced during their treatment. Then they submit it.

After that you go to user stories to view the stories submitted by
everyone. Except we are getting an error when we try to open this page
The error is posted below, and also the code from the models and
controllers and rhtml files. I REALLY would appreciate any help in
resolving this, so that I can put it into open beta, and then get it
out to hopefully help make the nation aware of what is going on within
the VA healthcare system.

Thank you for your time,

Dave

P.S. if you spot the problem could you provide easy to understand
steps to resolve it?

Error from the page:

NoMethodError in Admissions#index

Showing app/views/admissions/_admission.rhtml where line #2 raised:

You have a nil object when you didn't expect it!
The error occurred while evaluating nil.treatment_start

Extracted source (around line #2):

1: <div class="admission">
2: date: <%=admission.treatment.treatment_start-%><br/>
3: story: <%=admission.treatment.description-%>
4: <%= debug(admission) %>
5: <%= debug(admission.treatment)%>

_admission.rhtml

<div class="admission">
date: <%=admission.treatment.treatment_start-%><br/>
story: <%=admission.treatment.description-%>
<%= debug(admission) %>
<%= debug(admission.treatment)%>
</div>

treatment.rb (model)

class Treatment < ActiveRecord::Base
  acts_as_rateable

  belongs_to :user
  belongs_to :condition
  belongs_to :treatable, :polymorphic=>true
  belongs_to :doctor_type

  validates_presence_of :symptoms_start

  def temp_rating=(temp_rating)
    @rating=temp_rating
  end

  def temp_rating
    @rating
  end

end

admission.rb (model)

class Admission < ActiveRecord::Base
  has_one :treatment, :as=>:treatable
  belongs_to :hospital
end

treatments_controller.rb

class TreatmentsController < ApplicationController
  requires_authentication :using => :authenticate,
                          :include => [:new, :create, :update, :edit],
                          :realm => 'Unburdenus'
  # GET /treatments
  # GET /treatments.xml
  def index
    @treatments = Treatment.find(:all)

    respond_to do |format|
      format.html # index.rhtml
      format.xml { render :xml => @treatments.to_xml }
    end
  end

  # GET /treatments/1
  # GET /treatments/1.xml
  def show
    @treatment = Treatment.find(params[:id])

    respond_to do |format|
      format.html # show.rhtml
      format.xml { render :xml => @treatment.to_xml }
    end
  end

  # GET /treatments/new
  def new
    @user = User.current_user
    @treatment = Treatment.new
    @stylesheet_includes = ['reset-fonts-
grids','default','glenn','datebocks_engine', 'calendar-blue']
    @javascript_includes =
["prototype","scriptaculous",'moo.fx.js','accordion','application',"datebocks_engine",
"calendar", "lang/calendar-en", "calendar-setup","fckeditor/
fckeditor"]
  end

  # GET /treatments/1;edit
  def edit
    @treatment = Treatment.find(params[:id])
  end

  # POST /treatments/1;rate
  def rate
    if params[:id].nil?
      @treatment = Treatment.new
      @treatment.temp_rating=params[:rating]
    else
      @treatment = Treatment.find(params[:id])
      Rating.delete_all(["rateable_type = 'Treatment' AND rateable_id
= ? AND user_id = ?", @treatment.id, User.current_user.id])
      @treatment.add_rating Rating.new(:rating =>
params[:rating], :user_id => User.current_user.id)
    end
  end

  # POST /treatments
  # POST /treatments.xml
  def create
    @hospital = Hospital.find_or_create_by_name(params[:hospital]
[:name])
    @admission = Admission.new(:hospital=>@hospital)
    @treatment = Treatment.new(params[:treatment])
    @treatment.condition =
Condition.find_or_create_by_name(params[:condition][:name])
    @treatment.user = User.current_user
    @treatment.treatable = @admission
    respond_to do |format|
      if @treatment.save
        flash[:notice] = 'Treatment was successfully created.'
        format.html { redirect_to user_url(User.current_user) }
        format.xml { head :created, :location =>
treatment_url(@treatment) }
      else
        format.html {
          @stylesheet_includes = ['reset-fonts-
grids','default','glenn','datebocks_engine', 'calendar-blue']
          @javascript_includes =
["prototype","scriptaculous",'moo.fx.js','accordion','application',"datebocks_engine",
"calendar", "lang/calendar-en", "calendar-setup","fckeditor/
fckeditor"]
          render :action => "new"
          }
        format.xml { render :xml => @treatment.errors.to_xml }
      end
    end
  end

  # PUT /treatments/1
  # PUT /treatments/1.xml
  def update
    @treatment = Treatment.find(params[:id])

    respond_to do |format|
      if @treatment.update_attributes(params[:treatment])
        flash[:notice] = 'Treatment was successfully updated.'
        format.html { redirect_to treatment_url(@treatment) }
        format.xml { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml { render :xml => @treatment.errors.to_xml }
      end
    end
  end

  # DELETE /treatments/1
  # DELETE /treatments/1.xml
  def destroy
    @treatment = Treatment.find(params[:id])
    @treatment.destroy

    respond_to do |format|
      format.html { redirect_to treatments_url }
      format.xml { head :ok }
    end
  end
end

admissions_controller.rb

class AdmissionsController < ApplicationController
  # GET /admissions
  # GET /admissions.xml

  def index
    @admissions = Admission.find(:all)

    respond_to do |format|
      format.html {
        @stylesheet_includes = ['reset-fonts-grids','default','glenn']
      }
      format.xml { render :xml => @admissions.to_xml }
    end
  end

  # GET /admissions/1
  # GET /admissions/1.xml
  def show
    @admission = Admission.find(params[:id])

    respond_to do |format|
      format.html # show.rhtml
      format.xml { render :xml => @admission.to_xml }
    end
  end

  # GET /admissions/new
  def new
    @admission = Admission.new
  end

  # GET /admissions/1;edit
  def edit
    @admission = Admission.find(params[:id])
  end

  # POST /admissions
  # POST /admissions.xml
  def create
    @admission = Admission.new(params[:admission])

    respond_to do |format|
      if @admission.save
        flash[:notice] = 'Admission was successfully created.'
        format.html { redirect_to admission_url(@admission) }
        format.xml { head :created, :location =>
admission_url(@admission) }
      else
        format.html { render :action => "new" }
        format.xml { render :xml => @admission.errors.to_xml }
      end
    end
  end

  # PUT /admissions/1
  # PUT /admissions/1.xml
  def update
    @admission = Admission.find(params[:id])

    respond_to do |format|
      if @admission.update_attributes(params[:admission])
        flash[:notice] = 'Admission was successfully updated.'
        format.html { redirect_to admission_url(@admission) }
        format.xml { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml { render :xml => @admission.errors.to_xml }
      end
    end
  end

  # DELETE /admissions/1
  # DELETE /admissions/1.xml
  def destroy
    @admission = Admission.find(params[:id])
    @admission.destroy

    respond_to do |format|
      format.html { redirect_to admissions_url }
      format.xml { head :ok }
    end
  end
end

Hmmm… you’re getting that error because somehow, the @admission you’ve selected from the database does not have a treatment associated with it. It could be that you just have a leftover record in your system without a treatment.

You can overcome this several ways…

first, do a check in that partial

<% unless @admission.treatment.nil? %>
date: <%=admission.treatment.treatment_start-%>

story: <%=
admission.treatment.description-%>
<% end %>

Or you can take the lazy (but as I understand it, expensive) way out and just do
date: <%=admission.treatment.treatment_start rescue “No start date”-%>

story: <%=admission.treatment.description rescue “no story”-%>

I’m going to go out on a limb here and say that you have unit tests in place to ensure that your associations between treatments and admissions are working… so I bet it has something to do with your data.

Brian,

Thank you so much, I am now able to view that page, and start moving
towards final completion. I may push it out to open beta tonight so
that the rails newsgroup can view it and give some feedback.

Thank you,

Dave